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