23 design patterns (13) Java observer pattern
23 design patterns Part 13: Java observer pattern
Definition: defines a one to many dependency between objects, so that when each object changes state, all objects that depend on it will be notified and updated automatically.
Type: behavior class pattern
Class diagram:
In software systems, there is often a demand that if the state of an object changes, some objects related to it should also change accordingly. For example, we need to design a right-click menu function. As long as we right-click in the effective area of the software, a menu will pop up; For another example, we need to design an automatic deployment function, just like when eclipse develops, as long as the file is modified, eclipse will automatically deploy the modified file to the server. There is a similarity between the two functions, that is, an object should always monitor another object. As soon as its state changes, it should take corresponding actions. In fact, there are many schemes to achieve this, but there is no doubt that using the observer model is a mainstream choice.
Structure of observer model
In the most basic observer mode, it includes the following four roles:
Observed: as can be seen from the class diagram, there is a vector container used to store the observer object in the class (the reason why vector is used instead of list is because there are many
During thread operation, vector is safe while list is unsafe). This vector container is the core of the observed class. In addition, there are three methods: the attach method is to add an observer object to this container; The detach method removes the observer object from the container; The notify method calls the corresponding methods of the observer object in turn. This role can be an interface, an abstract class or a concrete class. In many cases, it will be mixed with other patterns, so there are many cases where abstract classes are used.
Observer: the observer role is generally an interface. It has only one update method. When the observer state changes, this method will be triggered and called.
Specific Observer: this role is used to facilitate extension. Specific business logic can be defined in this role.
Specific Observer: the specific implementation of the observer interface. In this role, the logic to be processed when the state of the observed object changes will be defined. Observer mode code implementation
Operation results
Observer 1 receives the information and processes it. Observer 2 receives the information and processes it.
From the running results, we can see that we only called the method of subject, but the relevant methods of the two observers were called at the same time. Take a closer look at the code. It's actually very simple. It's just to associate the Observer class in the subject class and traverse the observer's update method in the dosomething method.
Advantages of observer mode
The relationship between the observer and the observed is mild and abstract coupling, so it is easy to expand for both. Observer mode is a commonly used trigger mechanism. It forms a trigger chain and processes the methods of each observer in turn. But at the same time, this is also a disadvantage of the observer mode. Due to the chain trigger, when there are more observers, the performance problem is more worrying. Moreover, in the chain structure, the error of circular reference is easy to occur, resulting in the false death of the system.
summary
In the Java language, there is an interface observer and its implementation class observable, which implements the observer role. We can check the usage of these two classes in the JDK API document. Friends who have done VC + +, JavaScript DOM or AWT development are amazed at their event processing. When they understand the observer mode, they have a certain understanding of the principle of event processing mechanism. If you want to design the function of an event trigger processing mechanism, using observer mode is a good choice. The event processing DEM (delegation event model) in AWT is implemented using observer mode.
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.