Thursday, August 30, 2012

Observer Pattern


Motivation

The cases when certain objects need to be informed about the changes occured in other objects are frequent.
To have a good design means to decouple as much as possible and to reduce the dependencies.
The Observer Design Pattern can be used whenever a subject has to be observed by one or more observers

Let's assume we have a stock system which provides data for several types of client.
We want to have a client implemented as a web based application but in near future we need to add clients for mobile devices, Palm or Pocket PC, or to have a system to notify the users with sms alerts.

Now it's simple to see what we need from the observer pattern:
we need to separate the subject(stocks server) from it's observers(client applications) in such a way that adding new observer will be transparent for the server.


Intent

Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.


Applicability & Examples

The observer pattern is used when:
the change of a state in one object must be reflected in another object without keeping the objects tight coupled.
the framework we are writing needs to be enhanced in future with new observers with minimal changes.

Some Classical Examples:

    Model View Controller Pattern - The observer pattern is used in the model view controller (MVC) architectural pattern. In MVC the this pattern is used to decouple the model from the view. View represents the Observer and the model is the Observable object.

    Event management - This is one of the domains where the Observer patterns is extensively used. Swing and .Net are extensively using the Observer pattern for implementing the events mechanism.


http://www.oodesign.com/observer-pattern.html
http://sourcemaking.com/design_patterns




  • Example with java.util.observer class


Typical use cases:
- a mailing list, where every time an event happens (a new product, a gathering, etc.) a message is sent to the people subscribed to the list.
- Model/View/Controller: where the view needs to fetch/data information about the model or the model needs to notify the view of a change.

Imagine an airport system, that needs to notify its passengers of potential delays due to a natural occurrence such as a Volcanic Ash or other reasons.

http://www.dreamincode.net/forums/topic/252769-design-patterns-observer-pattern/







  • Observer design Pattern in Java with Real world code Example


You are free to choose java.util.Observable or java.util.Observer or writing your own Subject and Observer interface.

Advantage of Observer Design Pattern in Java:

Main advantage is loose coupling between objects called observer and observable.
The subject only know the list of observers it don’t care about how they have their implementations
All the observers are notified by subject in a single event call as Broadcast communication

Disadvantage of Observer Design Pattern in Java:

   Another issue is Memory management because subject will hold all the reference of all the observers if we not unregister the object it can create the memory issue

The disadvantage is that the sometime if any problem comes, debugging becomes very difficult because flow of control is implicitly between observers and observable we can predict that now observer is going to fire and if there is chain between observers then debugging become more complex


http://javarevisited.blogspot.com/2011/12/observer-design-pattern-java-example.html

No comments:

Post a Comment