【观察者模式】

/**
 * Description:
 * <br/>网站: <a href="http://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="http://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="http://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="http://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="http://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="http://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);
		}
	}
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值