Observer pattern


 
The observer pattern defines a relationship between a subject and one or more observer objects, which interact. Subjects (observables) update the observers using a common interface. Observers are losely coupled in that the Observable knows nothing about them, other than that they implement the Observer interface. You can pull data from the Observable when using the pattern. Don't depend on a specific order of notification for the observers.


Design principles (link):

  • (OK) Open/Closed Principle - You can introduce new observer subclasses without having to change the Observable code (and vice versa if there’s an Observable abstraction).
  • (OK) Interface Segregation Principle - The Observer pattern respects the ISP since the only method in the Observer interface, update( ) is supposed to be used by all its subclasses.

Relations with Other Patterns

  • MVC usually uses the Observer pattern to let the controller automatically update the view when the model notifies it of a change in its data.

Comments

Popular Posts