Let's look at the design pattern-Observer Pattern and design pattern observer pattern.
With the development of the soft test, we started a new round of design model learning, which has been some time away from the previous overall and from the start to the end of the design model. In the last overall learning, I mainly learned how to use the design pattern in a simple way. With this period of contact, the study is more profound. Next I will take the observer mode as an example to record the learning of this design mode. (With Head First as the main material ).
1. Review what is the observer Mode
Let's take a look at how newspapers and magazines subscribe. The newspaper business is to publish a newspaper to subscribe to a newspaper. As long as they have published a new newspaper, they will send it to you. As long as you are their subscribers, you will always receive new newspapers. When you don't want to read them again, they will not send new newspapers as long as they are still running, someone (or organization) will always subscribe to or unsubscribe to a newspaper. If you know what the newspaper subscription is, you actually know what the observer mode is, but the name is not the same: the publisher calls it a "topic ), the subscriber is changed to "Observer ). Observer mode, relationship:
2. Observer mode definition
When you try to outline the observer model, you can use the newspaper subscription service and compare the publisher with the subscriber. In the real world, you usually see that the Observer Model is defined:
A. The observer mode defines one-to-multiple dependencies between objects. In this way, when an object changes its status, all its dependent persons will receive notifications and update them automatically.
B. The observer mode defines the one-to-many relationship between a series of objects.
C. When an object changes its status, other dependent persons will receive a notification.
3. Observer pattern class diagram
When two objects are loosely coupled, they can still interact, but they are not clear about the details of each other. The observer mode provides an object design, so that the topic and the observer are loosely coupled. Why?
The topic only knows that the Observer has implemented an interface (that is, the Observer interface ). The topic does not need to know who the observer is, what it has done, or any other details.
We can add new observers at any time.
Because the only thing that a topic depends on is a list of objects that implement the Observer interface, we can add the Observer at any time. In fact, we can replace the existing Observer with a new Observer at runtime, the topic will not be affected. Likewise, some observers can be deleted at any time.
We can reuse theme or observer independently.
If theme or observer needs to be used elsewhere, they can be easily reused because they are not tightly coupled.
Changing the subject or one of the observers does not affect the other.
Because the two are loosely coupled, as long as the interfaces between them are still observed, we can change them freely.
4. Sample Code
Take the meteorological station as an example (the example is from head first design mode)
The class diagram is as follows:
The weather station subject code is as follows:
Implement the topic interface in WeatherData
Build an announcement board code
After the establishment is successful, you can establish the following test program
The following result is displayed:
Summary
So far, this design model has basically been improved. In a learning process, it is also a process of complementing and deepening. There are many problems that we could not understand, understand or understand before. In this study, we have basically solved these problems. Learning and reviewing once and again will certainly have good results. After all, learning is a process of repetition.