The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. When the subject object changes in state, all observer objects are notified so that they can automatically update themselves.
The main point here is to learn the records of the built-in observers, which in the Java language's Java.util library provides a observable class and a observer interface that forms the Java language's support for the Observer pattern.
Observer interface
This interface defines only one method, the update () method, which is called by the Notifyobservers () method of the Observer object when the state of the object being observed changes.
Observable class
The Observer class is a subclass of the Java.util.Observable class. Java.util.Observable provides a public way to support the Observer object, where two of these methods are important for observable subclasses: one is setchanged () and the other is Notifyobservers (). The first method setchanged () is called and an internal tag variable is set, representing the state of the object being observed to change. The second is notifyobservers (), which, when called, invokes the update () method of all registered observer objects so that the observer objects can update themselves.
Java design pattern 02 Watcher Pattern