When to use Observer Pattern


This details the definition, applicabiltiy, advantages and disadvantages of Observer Pattern. This details about when to use Observer pattern and when not to use it. The Observer is also known as Behavioural pattern. it also defined the one-to-many relationships

Defintion


When one object changes its state, all its observers(or dependents) are notified and updated automatically. This pattern used to maintain the consistency between the related objects. Observer Pattern is also called as "Publish - Subscribe" mechanism.

Applicability



  • Will be used when a change in one object requires a changing in another object.

  • When one object depend on the other

  • Widely used in GUI Applications(Model - View - Controller)

  • Use this pattern to reduce coupling


Disadvantages



  • Memory Leak: If domain objects observed other domain objects, When we are out of that scope we need to delete that domain objects. Because along with the objects it will hold the observer relation ship. Long lived unused objects may lead to memeory leak

  • It is not clear in advance how amny and which objects will be notified due to the state change

  • Return type has to be void

  • Update operations are asynchronous


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: