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-2016, 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 (Observer o : observers) { // 显式调用每个观察者的update()方法 o.update(this , value); } } }
/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, 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); }
/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, 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); } } }
import java.util.Iterator; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, 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); } }
/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, 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 Test { public static void main(String[] args) { // 创建一个被观察者对象 Product p = new Product("电视机" , 176); // 创建两个观察者对象 NameObserver no = new NameObserver(); PriceObserver po = new PriceObserver(); // 向被观察对象上注册两个观察者对象 p.registObserver(no); p.registObserver(po); // 程序调用setter方法来改变Product的name和price成员变量 p.setName("书桌"); p.setPrice(345f); } }
import javax.swing.JFrame; import javax.swing.JLabel; /** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> * <br/>Copyright (C), 2001-2016, 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); } } }