1. Overview
Sometimes referred to as the Publish/subscribe pattern, the Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. This subject object notifies all observer objects when the state changes, enabling them to automatically update themselves.
2. Issues to solve
Splitting a system into a class in which some classes work together has a bad side effect, which is the need to maintain consistency among related objects. We do not want to be tightly coupled in order to maintain consistency, which can cause inconvenience to maintenance, expansion, and reuse. The observer is the solution to this kind of coupling relationship.
3. Roles in the pattern
3.1 Abstract Theme (Subject): It saves references to all observer objects in a single aggregation, each subject can have any number of observers. Abstract topics provide an interface that allows you to add and remove observer objects.
3.2 Specific topics (ConcreteSubject): deposit the relevant state into a specific observer object, and give notice to all registered observers when changes are made to the internal state of the specific subject.
3.3 Abstract Observer (Observer): Define an interface for all specific observers and update yourself when you get a topic notification.
3.4 Specific Observer (CONCRETEOBSERVER): Implements the update interface required by the abstract observer role in order to reconcile its state with the subject state.
This selects the Apache Commons IO Monitor to analyze the observer pattern.
Observer-io (File Monitor)