Observer pattern
Defined
The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.When you're trying to picture the Observer Pattern, a newspaper subscription service with its publishers and subscribers is a good way to visualize the pattern.
In the real world however, you'll typically see the Observer Pattern defined like this.
The subject (the observed object) and the observers define the one to many relationship.
The observers are dependent on the subject such that when the subjects state changes, the observers are updated with new values.
Strive for Loose coupled design between objects that interact.
Loose coupling is an approach to interconnecting the components in a system or network so that those components, also called elements, depend on each other to the least extent practicable. Coupling refers to the degree of direct knowledge that one element has of another.
The goal of a loose coupling architecture is to reduce the risk that a change made within one element will create unanticipated changes within other elements.Don't wish to code concrete update method implementations, as we have to way of add or remove without making changes to the program.
Diagrams
Code Example
Bullet Points
- The Observer Pattern defines a one to many relationship between objects.
- Subjects, or as we also know them, Observables, update Observers using common interface.
- Observers are loosely coupled in that the Observable knows nothing about them, other than that they implement the observer Interface.
- You can push or pull data from the Observable when using the pattern (pull us considered correct)
- Dont depend on a specific order of notification for your observers.
No comments:
Post a Comment