-
Notifications
You must be signed in to change notification settings - Fork 0
Observer

Behavioral pattern
Defines one-to-many relationship between the Subject object and Observer objects, in which when Subject's state changes, all assigned Observer objects are getting notified and updated.
Subject object contains collection of Observer instances, and when notifyObservers method is called - it notifies each assigned Observer. When notified each Observer should adjust to new Subject state accordingly.
Advantages:
+ Allows to introduce loose coupling between Subject and Observer classes, since any ConcreteObserver is only binded via Observer interface. New type of ConcreteObserver can be introduced into system without any changes to a Subject class code.
+ Observers can be assigned to/removed from Subject(-s) at any point of runtime.
At this project we have the following model:
We have PostOffice class, which would be our Subject. PostOffice is able to store mail, which gets there from time to time. Besides storing mail, PostOffice has regular visitors in the shape of PostOfficeVisitor instances (our Observers).
When new mail gets to the PostOffice every assigned PostOfficeVisitor gets notified about it. When notified, PostOfficeVisitor checks if PostOffice has mail for them, and if it does, visitor picks it up.