日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

設計模式之二觀察者模式

系統 1748 0

?通俗的說:我們向報社訂閱報紙,這個就是觀察者模式的實例,我們是觀察者(Observer),報社就是主題(Subject)。

?

觀察者模式定義了對象之間的一對多依賴,這樣一來,當一個對象改變狀態時,它的所有依賴者都會收到通知并自動更新。

?

?現在我們設計一個氣象監測站系統,此系統中的三個部分分別是氣象站(獲取實際氣象數據)、布告板(顯示當前的天氣狀況)

?

、WeatherData(跟蹤來自氣象站的數據,并更新布告板),布告板可以提供擴展。

?

系統設計類圖如下:

?


設計模式之二觀察者模式
?

    /**
 * 主題
 * @author wengn
 *
 */
public interface Subject {
	public void registerObserver(Observer o);
	
	public void removeObserver(Observer o);
	
	public void notifyObserver();
}

/**
 * 觀察者
 * @author wengn
 *
 */
public interface Observer {
	public void update(float temp,float humidity,float pressure);
}

/**
 * 布告板接口
 * @author wengn
 *
 */
public interface DisplayElement {
	public void display();
}

/**
 * 主題實現類
 * @author Administrator
 *
 */
public class WeatherData implements Subject {
	private ArrayList observers;
	private float temperature;
	private float humidity;
	private float pressure;

	public WeatherData(){
		observers = new ArrayList();
	}
	
	public void notifyObserver() {
		for(int i=0;i<observers.size();i++){
			Observer o = (Observer)observers.get(i);
			o.update(temperature, humidity, pressure);
		}
	}

	public void registerObserver(Observer o) {
		observers.add(o);
	}

	public void removeObserver(Observer o) {
		int i = observers.indexOf(o);
		if(i>=0){
			observers.remove(i);
		}
	}
	
	public void measurementsChanged(){
		this.notifyObserver();
	}
	
	public void setMeasurements(float temperature,float humidity,float pressure){
		this.temperature = temperature;
		this.humidity = humidity;
		this.pressure = pressure;
		measurementsChanged();
	}
}

/**
 * 布告板實現類
 * @author wengn
 *
 */
public class CurrentConditionsDisplay implements Observer, DisplayElement {
	private float temperature;
	private float humidity;
	private Subject weatherData;
	
	public CurrentConditionsDisplay(Subject s){
		this.weatherData = s;
		weatherData.registerObserver(this);
	}

	public void update(float temp, float humidity, float pressure) {
		this.temperature = temp;
		this.humidity = humidity;
		display();
	}

	public void display() {
		System.out.println("show:"+temperature+"||"+humidity);
	}
}

/**
 * 測試類
 * @author wengn
 *
 */
public class Test {

	public static void main(String[] args) {
		WeatherData wd = new WeatherData();
		CurrentConditionsDisplay ccd = new CurrentConditionsDisplay(wd);
		wd.setMeasurements(0, 0, 0);
		wd.setMeasurements(1, 1, 1);
		wd.setMeasurements(2, 2, 2);
	}
}

  

?

? 附上java awt中觀察者模式的模擬代碼:

?

?

    package com.hanbing.awt;

import java.util.ArrayList;
import java.util.List;

public class MyAwt {
	public static void main(String[] args) {
		Button bt = new Button();
		MyActionLisenter lisenter = new MyActionLisenter();
		MyActionLisenter2 lisenter2 = new MyActionLisenter2();
		bt.addActionLisenter(lisenter);
		bt.addActionLisenter(lisenter2);
		bt.buttonPressed();
	}
}


class Button {
	private List<ActionLisenter> list = new ArrayList<ActionLisenter>();

	public void buttonPressed() {
		ActionEvent e = new ActionEvent(System.currentTimeMillis(), this);
		for (ActionLisenter lisenter:list) {
			lisenter.actionPerformed(e);
		}
	}

	public void addActionLisenter(ActionLisenter lisenter) {
		list.add(lisenter);
	}
}


interface ActionLisenter {
	public void actionPerformed(ActionEvent e);
}


class MyActionLisenter implements ActionLisenter {
	public void actionPerformed(ActionEvent e) {
		System.out.println("oh my god!!!");
	}
}


class MyActionLisenter2 implements ActionLisenter {
	public void actionPerformed(ActionEvent e) {
		System.out.println("oh my god2!!!");
	}
}


class ActionEvent {
	private long when;

	private Object source;

	public ActionEvent(long when, Object source) {
		super();
		this.when = when;
		this.source = source;
	}

	public long getWhen() {
		return when;
	}
	
	public Object getSource(){
		return source;
	}
}
  

?

設計模式之二觀察者模式


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 民乐县| 光泽县| 咸阳市| 梅河口市| 岱山县| 新营市| 基隆市| 文登市| 蓬安县| 罗城| 双江| 仙游县| 金溪县| 秦皇岛市| 深水埗区| 增城市| 长寿区| 涪陵区| 沙湾县| 兴仁县| 嘉定区| 滕州市| 武威市| 凤冈县| 西乌珠穆沁旗| 郎溪县| 普定县| 曲麻莱县| 资阳市| 平昌县| 南川市| 大余县| 北票市| 休宁县| 邵阳市| 菏泽市| 肇庆市| 满洲里市| 黄浦区| 白银市| 璧山县|