With the soft test, began a new round of design mode of learning, distance from the last time this whole, from beginning to end learning design pattern has been a while. The last time the overall study, mainly to understand, simple use of design patterns. With this period of contact, imperceptible, this study, relatively in-depth some. Let me take the observer pattern as an example to record the learning of this design pattern. (Head first design mode is the main material).
1. Review what is the Observer pattern
Let's look at how newspapers and magazines are subscribed: The business of a newspaper is to publish a newspaper to subscribe to a newspaper and send it to you as soon as they have a new newspaper published. As long as you're their subscriber, you'll always get a new newspaper. When you don't want to read the newspaper, cancel the subscription, they won't send a new newspaper. As long as the newspaper is still in operation, there will always be people (or units) subscribing to newspapers or unsubscribing from newspapers. If you know what a newspaper's subscription is, you know what the observer pattern is, but the name is not the same: the publisher is renamed "Subject" (Subject) and the Subscriber is renamed "Observer" (Observer). Observer patterns, Relationships:
2. Observer Pattern definition
When you try to outline the observer pattern, you can use the newspaper subscription service, as well as publishers and subscribers to compare it all in the real world, you will often see the observer pattern defined as:
A. The Observer pattern defines a one-to-many dependency between objects, so that when an object changes state, all its dependents are notified and automatically updated
B. The observer pattern defines a one-to-many relationship between a series of objects
C. When an object changes state, other dependents will receive notification
3. Observer Pattern class diagram
When two objects are loosely coupled, they can still interact, but it's not clear to each other that the detail viewer pattern provides an object design that lets the subject and the observer loose coupling why?
As for everything about the observer, the subject only knows that the observer implements an interface (that is, the Observer interface). The subject does not need to know who the observer is, what it does, or any other detail.
We can add new observers at any time.
Because the only thing that the topic relies 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 the new observer at runtime, and the subject will not be affected. Similarly, it is possible to delete some observers at any time.
We can reuse themes or observers on their own
If we need to use themes or observers elsewhere, we can easily reuse them because they are not tightly coupled.
Changing one of the subjects or the observer does not affect the other party
Because the two are loosely coupled, we are free to change them as long as the interfaces between them are still adhered to.
4. Code Examples
Take an example of a weather station (example from head first design mode)
The class diagram is as follows:
The weather station subject code is as follows
Implementing the theme interface in Weatherdata
Set up bulletin Board code
After successful establishment, the following test program can be established
You can get the following results
Summary
So far, this design pattern of learning is basically perfect. In a study, but also a supplement and deepen the process, there are many previously can not understand, bad understanding or do not understand the problem, in this study, basically solved. A lot of study and review, will certainly have a very good effect. After all, learning is the process of repetition.
Look at the design pattern again--observer pattern