646
技術社區[雲棲]
【觀察者模式】
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public interface Observer
{
void update(Observable o , Object arg);
}
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public abstract class Observable
{
// 用一個List來保存該對象上所有綁定的事件監聽器
List<Observer> observers =
new ArrayList<Observer>();
//定義一個方法,用於從該主題上注冊觀察者
public void registObserver(Observer o)
{
observers.add(o);
}
//定義一個方法,用於從該主題中刪除觀察者
public void removeObserver(Observer o)
{
observers.remove(o);
}
//通知該主題上注冊的所有觀察者
public void notifyObservers(Object value)
{
//遍曆注冊到該被觀察者上的所有觀察者
for (Iterator it = observers.iterator();
it.hasNext(); )
{
Observer o = (Observer)it.next();
//顯式每個觀察者的update方法
o.update(this , value);
}
}
}
import java.util.Iterator;
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class Product extends Observable
{
//定義兩個屬性
private String name;
private double price;
//無參數的構造器
public Product(){}
public Product(String name , double price)
{
this.name = name;
this.price = price;
}
public String getName()
{
return name;
}
//當程序調用name的setter方法來修改Product的name屬性時
//程序自然觸發該對象上注冊的所有觀察者
public void setName(String name)
{
this.name = name;
notifyObservers(name);
}
public double getPrice()
{
return price;
}
//當程序調用price的setter方法來修改Product的price屬性時
//程序自然觸發該對象上注冊的所有觀察者
public void setPrice(double price)
{
this.price = price;
notifyObservers(price);
}
}
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class NameObserver implements Observer
{
//實現觀察者必須實現的update方法
public void update(Observable o , Object arg)
{
if (arg instanceof String )
{
//產品名稱改變值在name中
String name = (String)arg;
//啟動一個JFrame窗口來顯示被觀察對象的狀態改變
JFrame f = new JFrame("觀察者");
JLabel l = new JLabel("名稱改變為:" + name);
f.add(l);
f.pack();
f.setVisible(true);
System.out.println("名稱觀察者:" +
o + "物品名稱已經改變為: " + name);
}
}
}
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class PriceObserver implements Observer
{
//實現觀察者必須實現的update方法
public void update(Observable o , Object arg)
{
if(arg instanceof Double)
{
System.out.println("價格觀察者:" +
o + "物品價格已經改變為: " + arg);
}
}
}
/**
* Description:
* <br/>網站: <a href="https://www.crazyit.org">瘋狂Java聯盟</a>
* <br/>Copyright (C), 2001-2012, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class PriceObserver implements Observer
{
//實現觀察者必須實現的update方法
public void update(Observable o , Object arg)
{
if(arg instanceof Double)
{
System.out.println("價格觀察者:" +
o + "物品價格已經改變為: " + arg);
}
}
}
最後更新:2017-04-04 07:03:36