3/14/2014

Observer Pattern

The observer pattern is a behavioral design pattern which defines a one-to-many relationship between objects. When the observable object changes its state, all dependent objects are notified and updated automatically. In stock market case, a stock trading system keeps track of all trades of specific stocks (subjects) and must inform all trader (observers) when a new trade has been made for new prices.

Word Definitions

Observer

someone who sees or notices something

Observable

something that is observable can be seen or measured

Code Example

Event-Bus

To be honest, I don’t want the Observable class to be tightly coupled with the classes that are interested in observing it. It doesn’t care who it is as long as it fulfils certain criteria. I think we might consider the event-bus if there are a lot of usage in your system. There are two reasons :

  • It replaces the tight coupling introduced by explicit listeners with a flexible loosely coupled design.
  • An event bus can be thought of like the observer pattern with an extra layer of decoupling.

Reference Link

No comments:

Post a Comment