Monday, December 30, 2013

Observer Design Pattern Example

Observer Design Pattern: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. -- By THE GOF BOOK

When to use? Observer design pattern is useful when we are interested in the state of an object and want to get notified whenever there is any change in the state of the object. In observer pattern, the object that watch the state of another object is called Observer and the object that is being watched is called Subject.

For example: In GUI programming, event-driven is an important concept. Such as, when you do programming in java UI /swing, you may put a button on the stage, then you’ll add a callback function to the button. The function will be called when there is some action play on the button, such as click. This is almost the same with the observer pattern.

Lets make it clear. Firstly, we care the state of the button; we want know when the button was clicked. Secondly, when the button state was changed, we want to do something, that’s the callback function. Actually, you can consider as the function cares the state of the button. When the button state change, the function will do something. So here the button is observable, and the function maybe the observer.

Keep in mind, An observable object can have one or more observers. An observer may be any object that implements interface Observer. After an observable instance changes, an application calling the Observable's notifyObservers method causes all of its observers to be notified of the change by a call to their update method.


Tuesday, November 19, 2013

Adapter Design Pattern Example

Adapter Design Pattern:  The adapter design pattern is a design pattern that translates one interface for a class into a compatible interface.


Adapter converts the interface of a class into another interface which is required or expect by clients. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Adapter also known as wrapper, an alternative naming shared with the Decorator pattern according to the GoF Design Patterns book.

Situations: During application development some time we face the following situations 

  • Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application. 
  • We can not change the library interface, since we may not have its source code. 
  • Even if we did have the source code, we probably should not change the library for each domain-specific application.

When to use: The Adapter pattern can be used when

  • You want to use an existing class, and its interface does not match the one you need
  • You want to create a reusable class that cooperates with unrelated classes with incompatible interfaces
Implementation Issues: How much adapting should be done?
  • Simple interface conversion that just changes operation names and order of arguments
  • Totally different set of operations


Thursday, October 31, 2013

Composite Design Pattern Example

Composite Design Pattern: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. --By GOF BOOK


A composite is an object designed as a composition of one-or-more similar objects, all exhibiting similar functionality and also shows the 1-to-many  relationship or parent child  hierarchy.

Problem: In an application if we need to represent a parent and child, node and leaf hierarchy or a tree structure. Then it is difficult to represent these structures in traditional way.

Solution: By using Composite pattern it is very easy to build these kind of hierarchy or tree structure.

This pattern is very useful, especially when you building a user interface. You can treat display object and display container object the same way, without considering the differences between the two objects. It’s very common in the GUI building environment.


Composite can be used when clients should ignore the difference between compositions of objects and individual objects. If you find that you are using multiple objects in the same way, and they often have nearly identical code to handle each of them, then composite is a good choice for you. it is less complex in this situation to treat primitives and composites as homogeneous (Node)...


Wednesday, October 9, 2013

Prototype Design Pattern Example

Prototype Design Pattern: Prototype design pattern is a creational design pattern and it is a template for creating an object from an already existing object. 

In this design pattern you use an already existing object as a prototype for creating a new object. If creating an object is time consuming, and if your application needs multiple such objects which are exactly the same or slightly different then you can use prototype pattern.

Prototype pattern can be used in transactional system, where in before performing a transaction, a clone of the prototype object is created, transaction is performed on the cloned object and if the transaction is successful  the prototype is replaced with cloned object by making the cloned object as the prototype. In case if transaction fails then the cloned object can be completely discarded.

The prototype object holds certain amount of information and if you need a similar object you can then clone the prototype object to create a similar object. If you application requires similar object but differ slightly then you can clone the prototype object and modify it slightly to fit the need of the application.