24-day design model-observer and 24-day Design Model

Source: Internet
Author: User

24-day design model-observer and 24-day Design Model
Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka
I. Observer Mode

1. Observer Mode Observer

The observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time.

When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.

2. Observer mode usage

(1) When an abstract model has two aspects, one of which depends on the other. Encapsulate the two in independent objects so that they can be changed and reused independently.

(2) When changing an object, you need to change other objects at the same time without knowing how many objects need to be changed.

(3) When an object must notify other objects, it cannot assume who the other objects are. In other words, you do not want these objects to be tightly coupled.

3. Composition of the observer Mode

Abstract topic role: Saves all references to the observer object in a collection. Each abstract topic role can have any number of observers. Abstract topic provides an interface to add and delete observer roles. It is generally implemented using an abstract class and interface.

Abstract observer role: Defines an interface for all specific observers and updates themselves when receiving notifications of a topic.

Specific topic roles: Send a notification to all registered observers when the internal status of a specific topic changes. A topic role is usually implemented using a subclass.

Specific observer role: This role implements the update interface required by the abstract observer role, so that its status can be consistent with that of the topic. It is usually implemented using a subclass. If necessary, a specific observer role can save a reference pointing to a specific topic role.

Ii. Example

The function of Meituan is simulated here. When you register an account on Meituan online, you will send an email to your mailbox whenever a new group is bought. Here, I changed it and sent it to the user whenever there is new food.

/*** File name: ObserverMethod. java * Description: Observer mode * created by: Lin bingwen * Date: 2015.1.27 **/package linbingwen; import java. util. arrayList;/* abstract target class */interface Subject {/* Registration Observer */public void RegisterObserver (Observer obj ); /* Delete the Observer */public void RemoveObserver (Observer obj);/* notify all observations */public void policyallobservers (String food );} /* abstract Observer class */interface Observer {public void update (String food );} /* specific target class */class RealSubject implements Subject {private ArrayList <Observer> UserList = new ArrayList <Observer> (); // used to store the Observer private ArrayList <String> Food = new ArrayList <String> (); // used to store any Food @ Overridepublic void RegisterObserver (Observer obj) {UserList. add (obj) ;}@ Overridepublic void RemoveObserver (Observer obj) {UserList. remove (obj) ;}@ Overridepublic void policyallobservers (String food) {for (Observer obj: UserList) {obj. update (food) ;}} class RealObserver implements Observer {private String name; // used to save the person's name // construct the public RealObserver (String name) {this. name = name ;}@ Overridepublic void update (String food) {System. out. println ("notification" + name + "" + food + "can be eaten");} public class ObserverMethod {public static void main (String [] args) {Subject MeiTuWang = new RealSubject (); // Meituan network // generates three Observer obj1 = new RealObserver (" "); observer obj2 = new RealObserver ("de"); Observer obj3 = new RealObserver ("a bing"); // register three observers MeiTuWang. registerObserver (obj1); MeiTuWang. registerObserver (obj2); MeiTuWang. registerObserver (obj3); // notify three observers that new foods can be used to eat MeiTuWang. policyallobservers ("apple"); MeiTuWang. notifyAllObservers ("pork"); MeiTuWang. notifyAllObservers ("rice ");}}



Output result: 

Notice xiaohongmeituan that Apple can be eaten this day
Notice that apsaradb for memcache can have an apple this day.
Inform Alibaba Cloud of the possibility of eating apples this day.


Notice Xiaohong Meituan that pork can be eaten this day
Notice that DRDs Meituan can eat pork this day
Inform abingmeituan that pork can be eaten this day


Notice xiaohongmeituan that rice can be eaten this day
Notice that DT Meituan has enough rice to eat this day.
Inform abingmeituan that there is rice to eat this day.


Iii. Advantages and Disadvantages of observer Mode

The following are some other advantages and disadvantages of the observer mode:
(1) Abstract coupling between a target and an observer only knows that a target has a series of observers, each of which is a simple interface of the abstract O B s e r v e r class. The target does not know which specific class any observer belongs. In this way, the coupling between the target and the observer is abstract and minimal. Because the target and the observer are not closely coupled, they can belong to different abstract layers in a system. A lower-level target object can communicate with a higher-level observer and notify it, thus maintaining the integrity of the system. If the target and the observer are mixed together, the obtained object must either traverse two layers (in violation of the hierarchy) or be placed in one layer of the two layers (which may damage the hierarchy abstraction ).
(2) Support for broadcast communication is not like a normal request, and the recipient of the notification sent by the target does not need to be specified. The notification is automatically broadcast to all objects registered with the target object. The target object does not care about how many objects are interested in itself. Its only responsibility is to notify its observers. This gives you the freedom to add and delete observers at any time. Processing or ignoring a notification depends on the observer.
(3) unexpected updates because an observer does not know the existence of other observers, it may have no idea about the final cost of changing the target. A seemingly harmless operation on the target may cause a series of updates to the observer and those objects dependent on the observer. In addition, if the dependency criterion is incorrectly defined or maintained, errors are often updated, which is usually difficult to capture. The simple update protocol does not provide specific details about what has been changed in the target, which makes the above problem even more serious. Without other protocols that help the observer discover what has changed, they may be forced to try their best to reduce the change.


Original works of Lin bingwen Evankaka. Reprinted please indicate the source http://blog.csdn.net/evankaka

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.