Wednesday, December 24, 2014

Observer Pattern

The Observer Pattern defines one-to-many relationship between objects so that when object changes state, all of its dependents are notified and updated automatically.

Observer Pattern fits in a scenario, where there is one Subject (object) and many Observers (dependent object). The main idea behind this design pattern is loose coupling.

Design Principle:
"Strive for loosely coupled designs between objects that interact.".

When two objects are loosely coupled, they can interact, but have very little knowledge about each other. The Observer Pattern provides an object design where subjects and observers are loosely coupled.

Because:
  • The only thing the subject knows about an observer is that it implements certain interface.
  • New observers can be added anytime.
  • No need to modify subject to add new types of observers.
  • Subjects and Observers can be reused independently of each other.
  • Changes to either the subject or an observer will not affect other.

Applications of Observer Pattern:

1. Weather Application
2. Stock Quotes
3. Newspaper subscription

No comments:

Post a Comment