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.

Example

  • 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.


Usage

  • It helps the objects to interact in a loosely fashion.
  • Any new observer can be added for any existing observable at any time without making any change in it.
  • The observable is completely abstract from the observers.

When to Use

  • When more than one object may be interested in the state of any object and this number may increase or decrease with time.

Drawbacks

  • A loss of even may occurs if observer is not active to observe the changes in observable or getting shut down due to any reason.
  • The system becomes very dynamic and sometimes debugging becomes a headache for developers in the absence of proper debugging strategies.

Strategy Pattern

Strategy pattern consists of a number of related algorithms encapsulated in a driver class named as context. User has the flexibility to choose any of the algorithms and can ask the context to apply this algorithm to current process.

The various algorithms should implement a common interface so that these can be invoked transparently.

Example

  • If want to save files in different formats based on users choice.
  • If we are designing a UI and generally save it in database on server using some remote bean. In test environment, we may need to save it locally on file system. Here we can define a context with both persistence systems and switch between these two based on the scenario.

Usage

  • It allows us to select one of the several algorithms dynamically.

When to use

  • When we have more than options to perform an operation and want to switch over these options depending on runtime scenarios.

Drawbacks

  • Different algorithms may require different number of parameters for their operation. To support this, the strategy interface should be broad enough to pass parameters which may not be required for one strategy but required for another.

Visitor Pattern

It provides a hook to one or more classes to act on the data of one class. It is against the OOP concepts but is required when we want to decouple commonly and repetitively used algorithms from a class to more generic classes. Here all or most of these classes, which are having some logic to act upon class data, can act on the class data.

What does ‘visiting’ mean? Generally an outside class can gain access to another class and that by calling its public methods. But here visiting each class means that you are calling a method already installed for this purpose, called accept. A visitor object is passed to this method, which if accepted by the class, its ‘visit’ method will be called passing itself as an argument.

Example

  • We have a collection of employees which comprises bosses as well. We want to calculate the total leaves taken by employees and bosses in last year. We can make two visitors one for only boss and other for employee not in a role of boss. Now simply iterate over collection and visit both visitors. Both visitors will calculate the respective leaves.


When to Use
  • When the program is in its mature stage and new classes are very unlikely.
  • When addition of new algorithms commonly required for a collection of classes
  • When we need to perform some  operations on a composite structure

Usage

  • It adds functionalities to a collection of classes.
  • It is easy to add new operations to an existing program since only visitor code needs to be changed, which contains the logic.

Drawbacks

  • It can’t act upon private methods so sometimes it might force to expose data through public methods.
  • Visitors are less helpful during the program’s growth state because for every new class to which visitor needs to visit, a new methods have to be added to the visitor to match the signature with this new class.





People who read this post also read :



0 comments:

Post a Comment