Adapter design pattern

Adapter pattern adapts a piece of code to work with another. In real world, consider a battery charger for your phone. If you went for vacation to a somewhere in Europe, your charger won’t fit into the wall outlet. You would need a piece that can wrap your that fits into the socket in Europe. Adapter pattern is usually helpful to create new code that is compatible with old legacy code. Using adapter pattern the old code do not need to be changed to work with new code. [Read more…]

Singleton Pattern

In Singleton Pattern, the class can only create a single instance. We want a class to have only a single instance for various reasons. Sometimes, we want use a global object to keep information about your program. This object should not have any copies. This information might be things like configuration of the program, or a master object that manages pools of resources. So when you need a resource, you ask the master object to get it for you. Now if there were many copies of this master object, you would not know whom to ask for that resource. This single object should not be allowed to have copies. Singleton Pattern forces this rule so that programmer doesn’t have to remember about not creating copies. Singleton pattern will create an instance if it doesn’t exist and will not create any new instance if an instance already exist. It will just return a reference to that single instance.
class ProgramConfiguration{
    public ProgramConfiguraiton(){
        //default constructor code
    }
}
[Read more…]

Factory method design pattern

OpenOffice.org development heavily uses the Factory method design pattern. Design patterns are conventional templates that describes how to solve common software problems. Since most developers are familiar with the patterns, they can recognize a pattern in others source code. That makes working in teams easier. There are many popular design patterns. One of them is Factory method pattern. Factory method pattern is a type of creational pattern. Creational pattern pattern solves problems related to creating. Factory pattern solves two major problem generally faced by developers. [Read more…]

Reading ‘Head First Design Patterns’

It is sad that CPA in Seneca doesn’t offer any course about Software Design Patterns. I just read through chapter two about ‘Subject Observers Pattern’. Subject Observers pattern is useful when a bunch of observer objects need to be notified about change in the subject object. Observers registers themselves to get notifications from the Subject. When there is a change in any data in Subject, the subject notifies all the observers about the change. The subject object maintains a list of all the observer objects. When an observer unregisters itself, it is removed from this list. So next time, the subject notifies all the observers in its list, the unregisters are not sent any more notifications. For example, a weather tracking system that sends temperature information to all the clients every time the temperature changes.