Practical Design Pattern-Observer Pattern

Source: Internet
Author: User

1. What is the observer mode?
The observer pattern is a type of software design pattern. In this mode, a target object manages all observer objects that depend on it and actively sends notifications when its status changes.

2. Observer mode implementation
Although the ready-made Observer and Observable implementations are provided in Java, since Observable has been implemented into a class, when you need to implement a certain service class of your own as an observed feature, we often have to implement it on our own. Therefore, we will not use the Observable and Observer classes in Java, but implement it on our own.

The usual method is registration-Notification-cancellation of registration. The Code is as follows:


[Java]
Public interface Listener {
Void onNotify ();
}
 
 
Public class Subject {
Private List <Listener> listeners;
Private Object value;

Public Subject (){
Listeners = new ArrayList <Listener> ();
Value = new Object ();
}

Public void registe (Listener listener ){
Listeners. add (listener );
}

Public void unRegiste (Listener listener ){
Listeners. remove (listener );
}

Private void sendpolicy (){
For (Listener listener: listeners ){
Listener. onNotify ();
}
}

Public Object getValue (){
Return value;
}

Public void setValue (Object value ){
If (! This. value. equals (value )){
This. value = value;
SendNotify ();
}
}
}

Public interface Listener {
Void onNotify ();
}


Public class Subject {
Private List <Listener> listeners;
Private Object value;
 
Public Subject (){
Listeners = new ArrayList <Listener> ();
Value = new Object ();
}
 
Public void registe (Listener listener ){
Listeners. add (listener );
}
 
Public void unRegiste (Listener listener ){
Listeners. remove (listener );
}
 
Private void sendpolicy (){
For (Listener listener: listeners ){
Listener. onNotify ();
}
}
 
Public Object getValue (){
Return value;
}
 
Public void setValue (Object value ){
If (! This. value. equals (value )){
This. value = value;
SendNotify ();
}
}
}

 

2. 1. Container Selection
The observer needs a container to retain the reference of the registered observer. The above Code uses List as the container. The selection of this container will affect the overall implementation scheme.

Generally, an Array or List container is used for storage. On the one hand, it can be ordered, and on the other hand, it can be easily traversed during notification.

If you do not want repeated observer references, you can either check whether they exist in the container at registration. For example, add contains () to the preceding registe () method, select non-repeated containers, such as Set and Map. Such a container already contains repeated processing, but it also means that the order of sending notifications is not the order of registration.

2. How to obtain data after notification
That is to say, 'push' or 'pull '. The push mode means that the changed data is passed to the observer as a parameter when called to the observer interface. The pull method is to say that the interface call to the observer is only used to notify the change of data. The observer calls the getXXX () method of the observer to obtain the data of interest. The former will push the changes to the observer no matter how useful you are. The latter will only fulfill the obligation of notification, and you will take the data you are interested in. This should be decided based on the actual design.

The pull method is used in the above Code. When the value changes, a notification is sent to the Listener through the sendpolicy () method, but the value is not carried in the notification, instead, the Listener party calls the getValue () method as needed to pull the new value after the change.

. Canceled registration after notification
The cancellation after notification is not as simple as the above sample code in actual application. A typical requirement is that a Listener calls unRegiste (this) during the onNotify () method execution ). To meet this requirement, you can handle the sendNotify () method with caution. You can also identify the sendNotify () method by using a flag. You can also traverse a copy instead of using the original container in sendNotify, in any case, this requirement is easily met and must be addressed when implementing the observer mode.

2. 4. Other problems
If the Subject is a time-related service, it is necessary to restrict the running time of each listener, and monitor and process the call time of each onNotify, to keep the time of each Listener distributed within a reasonable range. Or call onNotify () in asynchronous and multi-thread mode ().

If Subject needs to implement priority-based notifications, it is necessary to manage the priority queue and schedule notifications.

3. Summary
The observer mode is much more useful, but in actual work, we need to think deeply rather than simply copy the sample code. This is what the design mode gives us and we need to learn to turn it into our own experience.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.