Learn a little design pattern every day-observer mode

Source: Internet
Author: User
Tags constructor stub
Observer Pattern


English name


Observer pattern



definition


Defines a one-to-many dependency between objects, so that when an object changes state, all its dependents are notified and updated automatically.


Design Principles


1. Strive for loosely coupled design between interacting objects


Understanding


Objects and observers are abstracted as interfaces, and three methods are defined in an object interface: Adding observers, deleting observers, and notifying observers. There is only one method in the Observer interface that updates the data method. The class that implements the object interface has an array of saved observers. An object that inherits the constructor method of the subclass of the Observer interface.


Code


Object Class (Viewer)


Public interface Subject {public
	
	void Registerobserver (Observer o);

	public void Removeobserver (Observer o);

	public void Notifyobservers ();
}

Observers


Public interface Observer {public
	void update (float temp, float humidity, float pressure);
}


Performance Behavior Interface


Public interface Displayelement {public
	void display ();


Subclass of Object


public class Weatherdata implements Subject {private ArrayList observers;
	private float temperature;
	private float humidity;

	private float pressure;
	Public Weatherdata () {//TODO auto-generated constructor stub observers = new ArrayList (); }/* * (NON-JAVADOC) * * @see * com.sprd.pattern.observer.impl.subject#registerobserver (Com.sprd.pattern *. O)
		Bserver.impl.Observer) */@Override public void Registerobserver (Observer o) {//TODO auto-generated method stub
	Observers.add (o); }/* * (NON-JAVADOC) * * @see * com.sprd.pattern.observer.impl.subject#removeobserver (Com.sprd.pattern *. obs)
		Erver.impl.Observer) */@Override public void Removeobserver (Observer o) {int i = Observers.indexof (o);
	if (i >= 0) observers.remove (o); }/* * (NON-JAVADOC) * * @see com.sprd.pattern.observer.impl.subject#notifyobservers () */@Override public VO ID notifyobservers () {//TODO auto-generated method stub for (int i = 0; i < OBServers.size ();
			i++) {Observer Observer = (Observer) observers.get (i);
		Observer.update (temperature, humidity, pressure);
	}} public void Measurementschanged () {notifyobservers ();
		} public void setmeasurements (float temperature, float humidity, float pressure) {this.temperature = temperature;
		this.humidity = humidity;
		This.pressure = pressure;
	Measurementschanged ();
 }
}

3 observers


public class Currentconditionsdisplay implements Observer, displayelement {
	private float temperature;
	private float humidity;
	Private Subject Weatherdata;

	Public Currentconditionsdisplay (Subject weatherdata) {
		this.weatherdata = weatherdata;
		Weatherdata.registerobserver (this);
	}

	/
	 * * (non-javadoc) */
	@Override public
	void display () {
		System.out.println (String.Format (
				"Current conditions:%f f degrees and%.0f percent humidity",
				temperature, humidity));
	}
	 /* * (NON-JAVADOC) */
	@Override public
	void update (float temp, float humidity, float pressure) { C21/>this.temperature = temp;
		this.humidity = humidity;
		Display ();
	}

}

public class Forecastdisplay implements Displayelement, Observer {
	private float temperature;
	private float humidity;
	Private Subject Weatherdata;

	Public Forecastdisplay (Subject weatherdata) {
		this.weatherdata = weatherdata;
	}
	 /* * (NON-JAVADOC) * * 
	 @see com.sprd.pattern.observer.impl.observer#update (float, float, float) */
	@Override public
	void update (float temp, float humidity, float pressure) {
		//TODO auto-generated method s Tub
		this.temperature = temp;
		this.humidity = humidity;
	}
	 /* * (NON-JAVADOC) * * 
	 @see com.sprd.pattern.observer.impl.displayelement#display () */
	@ Override public
	void display () {
		//TODO auto-generated method stub

	}

}

public class Statisticsdisplay implements Displayelement, Observer {public
	
	statisticsdisplay (Subject weatherdata) {
		
	}

	/* (non-javadoc)
	 * @see com.sprd.pattern.observer.impl.observer#update (float, float, float) */
	@Override public
	void update (float temp, float humidity, float pressure) {
		//TODO auto-generated method stub

	}

	/* (Non-javadoc)
	 * @see com.sprd.pattern.observer.impl.displayelement#display () */
	@Override
	public void display () {
		//TODO auto-generated method stub

	}

}

Test program


public class Weatherstation {public

	static void Main (string[] args) {

		Weatherdata weatherdata = new Weatherdata () ;
		Currentconditionsdisplay currentdisplay = new Currentconditionsdisplay (
				weatherdata);
		Statisticsdisplay statisticsdisplay = new Statisticsdisplay (weatherdata);
		Forecastdisplay forecastdisplay = new Forecastdisplay (weatherdata);
		Weatherdata.setmeasurements (30.4f);
		Weatherdata.setmeasurements (29.2f);
		Weatherdata.setmeasurements (29.2f);
		
	}

}


Show results


Current conditions:80.000000 f degrees and, humidity current
conditions:82.000000 f degrees and humidity Current
conditions:78.000000 F degrees and humidity



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.