More Behavioral Design Patterns

Observer Pattern, Strategy Pattern, Visitor Pattern

Observer Pattern

It enables one or more objects to observe the states of one or more other objects. By implementing this pattern, one object request other object to intimate it for any change in a specific state which enables the system to refresh or act dynamically based on any state change anywhere in the system.

Here the object which is producing the events for state change is called observable and the object capturing the events is called observer.


  • If an object contains the data for system and two different objects like JList and JTable are responsible to display this data in required format. Here JList and JTable can register themselves as observer to object containing the data.  So that any change in data can be captured and corresponding change can be made in view.

Behavioral Design Patterns

These patterns are more specifically concerned with communication between objects.

Here are Chain of Responsibility Pattern, Iterator Pattern, Command Pattern and Mediator Pattern.

Chain of Responsibility

It allows a number of classes to attempt to handle a request, without any of them knowing about the existence or capabilities of other classes. So a lose coupling occurs between these classes and the request object passed to these classes is the only common link.

The request is passed through these classes until any one of these handles this request. Generally once handled, no other classes are visited. But a distorted form of this pattern allows more than one class to handle the request and process it, like servlets filters.


  • A help system, which can take the request for help from specific portion of UI. A chain of different help handlers in help system can try to handle this request and if any handler is successful to handle this request, it will do the processing. Further no handler will be invoked by core system.
  • We have the requirement to process some of the data from flat files and based on this data and some other parameters to the system, have to feed this data to database. The processing flow may vary depending on type of data and supplied parameters. Here distorted form of chain of responsibility can be applied.

Decorator Design Pattern

A decorator also known as wrapper is an object that has an interface identical to an object it contains. Any call that the decorator gets, it relays to the wrapped object and add its own functionalities along the way, either before or after the call. 


  • If we need to draw different borders for toolbar buttons, and one border may be applicable to more than one button. This can extend beyond border i.e. to other functionalities like color, font size etc.
  • Suppose if we are developing our data access system and want to track each and every database call made by using java.sql objects, we can define decorator connection, statement and result set which can wrap any other data base object and can intercept the calls made by these. 

Structural Design Patterns

Adapter Pattern, Bridge Pattern, and Composite Pattern

Adapter Pattern

Adapter is used to adapt the behavior/functionalities of an existing class but with a different interface.
It can be implemented by two ways, by inheritance (also called class adapter) or by composition (also called object adapter). Either we can extend the existing class and use its functionalities in new interface method, or we can use a reference of existing class in new object for using existing functionalities. 


  • We are making data bound UI components, which are implementing some of our proprietary interface to support the data loading/unloading. But UI components are already defined with Swing library so we want to use these and just want to bind them with data sources. Here we can extend our new components from existing Swing component and implement our data binding interface to it.
  • Use of Window Adapter while intercepting the window closing event. 

More Creational Design Patterns

Builder Pattern, and Prototype Pattern

Builder Pattern:

It separates the construction of complex object from its representation, so that number of different representations can be build based on the requirement. It is similar to factory pattern, but it does not return simple descendant of base class. The factory is concerned with what is made and the builder with how it is made. Factory simply returns an instance of related class of base type, but builder returns a composition of various classes depending on the specified state. 


  • Editor Builder: Suppose you are creating a UI for workflow where a number of different types of nodes can exist. We need different editors to edit these nodes, which can be very complex in structure. Here we can use builder which can build the desired editor.  Even editors can vary in structure based on current state of system.