"Onlookers" design pattern (19)--Behavioral observer pattern (Observer pattern)

Source: Internet
Author: User

The Observer pattern is one of the software design patterns. In this mode, a target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes. This is usually done by calling the methods provided by each observer. This mode is often used for real-time event processing systems. ----WIKIPEDIA


Personal Understanding

The observer pattern is to make the object instance of the observer in the Observer, in the event of some events, through the Notify "notify" the Observer, to complete the corresponding operation, he also called the Publish-subscribe pattern, defines a pair of multiple dependencies between objects, so that the object of the observer to generate action, You can notify the object that it depends on. The observed person has changed.


Case Analysis

just a few days ago bought two Brazilian turtle, take this as an example, the turtle will also be hungry, he is hungry I will go to feed it to eat, such words, can say exactly with the observer pattern. Push the way

Public interface Observable {

	//Add observer public
	void Addobserver (Observer Observer);
	Delete Observer public
	void Removeobserver (Observer Observer);
	Notifies the Observer public
	void Notifyobservers (String info);

Public interface Observer {

	//Fed public
	void feed (String info);

public class Turtle implements observable{

	private list<observer> observerlist = new Arraylist<observer > ();
	
	public void Hungry () {
		this.notifyobservers ("I'm hungry.") ");
	}

	@Override public
	void Addobserver (Observer Observer) {
		observerlist.add (Observer);
	}

	@Override public
	void Removeobserver (Observer Observer) {
		if (observerlist! = null && Observerlist.contains (Observer)) {
			observerlist.remove (Observer);
		}
	}
	
	@Override public
	void Notifyobservers (String info) {for
		(Observer observer:observerlist) {
			Observer.feed (info);}}}


public class XX implements Observer {

	@Override
	the public void feed (String info) {
		System.out.println ("feed. ");
	}
	
}

The situation here is that the Turtle class knows exactly what it wants to tell the observer, so this approach can be seen as a push, but not necessarily knowing what the other person wants, and at this point a pull is needed, that is, to pass an instance of itself to the Observer, It is up to the observer to decide what to acquire and what to pull.


Pull the way

The main changes are as follows:

public class Turtle2 implements Observable2 {

	private list<observer2> observerlist = new arraylist< Observer2> ();
	
	public void Hungry () {
		this.notifyobservers ();
	}

	@Override public
	void Addobserver (Observer2 observer) {
		OBSERVERLIST.ADD (Observer);
	}

	@Override public
	void Removeobserver (Observer2 observer) {
		if (observerlist! = null && Observerlist.contains (Observer)) {
			observerlist.remove (Observer);
		}
	}
	
	@Override public
	void Notifyobservers () {A (
		Observer2 observer:observerlist) {
			observer.feed (this) ;
		}
	}

	@Override Public
	Boolean isneedfeed () {
		return true;
	}
}

the advantages of the Observer pattern

1. The observer and the observer are abstract coupling: it is easier to extend the observer and Observer.

2. Set up a trigger mechanism, through a series of triggering mechanism, to form a trigger chain.


the disadvantage of the Observer pattern

Multi-level triggering, the efficiency is worrying, in the design of the time to fully consider this point.


scope of application of the Observer pattern

1. Associated Behavior scenarios.

2. Multi-level triggering of events.

3. Cross-system message exchange scenarios, the processing mechanism of Message Queuing.


Source code Download

Design mode source code download address


Recommended Reading

"Onlookers" design pattern (18)--Behavioral Template Method mode (Templatemethod pattern)

"Onlookers" design pattern (16)--the combination pattern of structure type (Composite pattern)


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.