A simple understanding of the observer pattern of design patterns

Source: Internet
Author: User

Design patterns are not as difficult as we think. We carefully understand the concept of a good design pattern, know what it is to solve the problem of this to better grasp. Let's look at the Observer pattern in the design pattern below.

(1) The concept of the Observer model

Defines a one-to-many dependency between objects, and when the state of an object changes, all objects that depend on it are notified and automatically updated.
(2) To understand the concept of good observer model we have to decompose the digestion of this sentence, the first definition of the object of a one-to-many relationship, in the programming development process, we generally how to express a one-to-many, is not through the collection class to achieve it. Then, when the state of an object changes, all objects that depend on him are notified and updated automatically. Here are two factors, the first is that the state of the object has changed, that is, the attributes of the object have changed, and the second is to notify the object of dependence upon change. (3) through the code to detail the realization of our understanding process Step 1: Create our Subject object, I prefer to call it the Boss object.

/**
 * Boss Object (Subject Object)
 * * *
/public class Subject {
	
	//Since is the eldest brother, that must have the younger brothers
	private list<observer> Observer = new arraylist<observer> ();
	The state of the boss
	private int;
	
	public int getState () {return state
		;
	}
	When the state of the boss changes, you have to inform the younger brother to do things. Public
	void SetState (int state) {
		this.state = state;
		Notifyallobserver ();
	}
	Every day there are new boys to join. Public
	void Attach (Observer Observer) {
		this.observer.add (Observer);
		
	}
	There are announcements to notify each younger brother public
	void Notifyallobserver () {for
		(Observer observer:this.observer) {
			Observer.update ();}}
Step 2: Observer (Object), I usually like to call him a little brother template (to join we must obey the rules)
Public abstract class Observer {
	 //Heart to have eldest brother
	 protected Subject Subject;
	 Accept the boss's prompt, execute action
	 abstract void Update ();
}
Step 3: Below is a variety of younger brothers, haha
public class Firstobserver extends observer{public
	
	firstobserver (Subject Subject) {
		this.subject = Subject;
		This.subject.attach (this);
	}
	@Override
	void Update () {
		System.out.println () +this.subject.getstate () +19);
		
	}

public class Secondobserver extends observer{public
	
	secondobserver (Subject Subject) {
		this.subject = Subject;
		This.subject.attach (this);
	}

	@Override
	void Update () {
		System.out.println () +this.subject.getstate () +19);
		
	}
	

Step 4: Brother says he's going to work.
public class Testobserver {public
	
	static void Main (string[] args) {
		Subject Subject = new Subject ();
		Firstobserver f = new firstobserver (subject);
		Secondobserver s =new secondobserver (subject);
		F.update ();
		S.update ();
		Subject.setstate (+);
		
	}

Result: The current state is3919 the current state is3919 the current state is88919 the current state is88919
Well, in fact this is the case, the specific understanding must be combined with the development experience. There is no place to welcome the exchange of advice.









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.