观察者模式用于描述对象之间的依赖关系,它引入了观察者和观察目标两类不同的角色,由于提供了抽象层,它使得增加新的观察者和观察目标都很方便。观察者模式广泛应用于各种编程语言的事件处理模型中,Java语言也提供了对观察者模式的全面支持。
抽象目标类
public abstract class Subject { protected ArrayList observers<Observer> = new ArrayList(); public void attach(Observer observer) { observers.add(observer); } public void detach(Observer observer) { observers.remove(observer); } public abstract void notify( ); }
具体目标类
public class ConcreteSubject extends Subject { //实现通知方法 public void notify() { for(Object obs:observers) { ((Observer)obs).update(); } } }
抽象观察者类
public interface Observer { public void update(); }
具体观察者类
public class ConcreteObserver implements Observer { public void update() { …… } }
股票
抽象目标类:股票
public interface Stocks { ArrayList<Observer> OBSERVERS = new ArrayList<>(); void attach(Observer observer); void detach(Observer observer); void notifyInvestor(); }
目标类:股票
public class Stock implements Stocks{ private String stockName; private int price; public Stock(String stockName, int price) { this.stockName = stockName; this.price = price; } public Stock() { } public String getStockName() { return stockName; } public void setStockName(String stockName) { this.stockName = stockName; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public void attach(Observer observer) { OBSERVERS.add(observer); } @Override public void detach(Observer observer) { OBSERVERS.remove(observer); } @Override public void notifyInvestor() { if(price >= 25*1.05 || price <= 25*0.95){ System.out.println("通知:股票价格变动幅度超过5%!"); for (Observer observer: OBSERVERS) { observer.upDate(); } } else { System.out.println("股票价格变化幅度没有超过5%!!"); } } }
抽象观察者类
public interface Observer { void upDate(); }
具体观察者类A
public class ConcreteObserverA implements Observer { private String name; private Stock stock; public ConcreteObserverA(String name, Stock stock) { this.name = name; this.stock = stock; } public ConcreteObserverA(String name) { this.name = name; } @Override public void upDate() { System.out.println("通知"+name+":"+stock.getStockName()+"的股票价格变化幅度超过5%,股票价格为:"+stock.getPrice()); } }
具体观察者类B
public class ConcreteObserverB implements Observer { private String name; public ConcreteObserverB(String name, Stock stock) { this.name = name; this.stock = stock; } public ConcreteObserverB(String name) { this.name = name; } private Stock stock; @Override public void upDate() { System.out.println("通知"+name+":"+stock.getStockName()+"的股票价格变化幅度超过5%,股票价格为:"+stock.getPrice()); } }
具体观察者类C
public class ConcreteObserverC implements Observer { private String name; private Stock stock; public ConcreteObserverC(String name, Stock stock) { this.name = name; this.stock = stock; } public ConcreteObserverC(String name) { this.name = name; } @Override public void upDate() { System.out.println("通知"+name+":"+stock.getStockName()+"的股票价格变化幅度超过5%,股票价格为:"+stock.getPrice()); } }
测试类
public class Test { public static void main(String[] args) { System.out.println("设计模式,2020006924,于鑫"); Stock stock1 = new Stock("股票1",25); System.out.println(stock1.getStockName()+"的股价为:"+stock1.getPrice()); Observer obs1,obs2,obs3; obs1 = new ConcreteObserverA("smith",stock1); obs2 = new ConcreteObserverB("tom",stock1); obs3 = new ConcreteObserverC("李白",stock1); stock1.attach(obs1); stock1.attach(obs2); stock1.attach(obs3); stock1.setPrice(22); stock1.notifyInvestor(); } }