The Observer Pattern

原创 hamo  2007-03-13 10:27  阅读 1,152 views 次

Observer - defines a ont-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

OO Principles

  • Strive for loosely coupled designs between objects that interact.

Bullet Points

  • The Observer Pattern defines a ont-to-many relationship between objects.
  • Subjects, or we also known them, Observables, update Observers using a common interface.
  • Observers are loosely coupled in that the observalbe knows nothing about them, other than that they implement the Observer interface.
  • You can push or pull the data of the Observalbe when using the pattern(pull is considered more "correct").
  • Don't depend on a specific order of notification for you Observers.
  • Java has several implementations of the Observer Pattern, including the gerneral purpose jave.util.Observable.
  • Watch out for issues with the java.util.Observable implementation.
  • Don't be afraid to create our own Observable implementation if needed.
  • Swing makes heavy use of the Observer Pattern, as do many GUI frameworks.
  • You'll also find the pattern in many other places, incluing JavaBeans and RMI.
版权声明:本文为原创文章,版权归 hamo 所有,欢迎分享本文,转载请保留出处!