Thursday 24 April 2014

Observer Design Pattern

What???
  • Observer pattern comes under Behavioral Design Pattern ,As this patterns deals with How the objects and classes sends messages to each other....
  • In observer design pattern multiple observer objects registers with a subject for change notification. When the state of subject changes, it notifies the observers. Objects that listen or watch for change are called observers/listeners and the object that is being watched for is called subject.
  • Observer pattern is used when there is one to many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically.



Applicability

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.

Example:

  • Lets assume that 3 or 4 users want to buy a product in one website called flipcart.
  • But currently that product is not available/out of stock state and It provide a option like NotifyMe When Product available.So These users will register into this site for notification.
  • After few days The Product is available in that website,so Perticular Notify method will send the notification to the all Registered users via email that the product they requested is availableNow and you can shop now.

    Note that in this example subject : Product Information,Observers:Users




Implimentation :


                     will update soon.......

No comments:

Post a Comment

test