The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
Being Loosely Coupled
- Subjects and observers are "loosely coupled"
They interact,
but have little knowledge of each other.
- Subject only knows
The observer implements a specific interface.
Does not need to know concrete class or anything else.
The Observer
- Subject relies on a list of observers.
- Observers can be:
+ Added at any time.
+ Removed at any time.
+ Or even replaced.
- Subject does not care, it keeps doing its job.
If we want to add new type of observers, modification of the subject is never needed.
- Observers just need to implement the Observer interface.
In fact, no changes to the subject or observers will affect the other.
> That's loose coupling.
Because object interdependencies are minimized, loose coupling allows us to build more flexible OO architectures.
Design Principle #4
Strive for loosely coupled designs between objects that interact.
댓글 영역