Observer pattern - Design Patterns
The observer pattern is a behavioural pattern. A one-to-many relationship between objects is created so that when the state of one object changes all its children objects are notified and updated automatically.
Pattern type
This is a behavioural pattern.
Intent
A one-to-many relationship between objects is created so that when the state of one object changes all its children objects are notified and updated automatically.
The pattern intent is to make it easier to maintain consistency between related objects without making them tightly coupled.
Problem
You have many clients that need to be updated when the state of of object changes.
Pattern
- the
Subject
class is the object that is being observed - the
Observer
class is the object that is observing theSubject
- the
ConcreteSubject
implements theSubject
interface - the
ConcreteObserver
can be any class that implements theObserver
interface
Code example
Consider this psuedo code example: