大话设计模式-观察者模式
基本简介:观察者<Observer>模式(有时又被称为发布-订阅<Publish/Subscribe>模式、模型-视图<Model/View>模式、源-收听者<Source/Listener>模式或从属者<Dependents>模式)是软件设计模式的一种。在此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。这通常透过呼叫各观察者所提供的方法来实现。此种模式通常被用来实作事件处理系统。
Demo为老板回来了,前台秘书通知办公室里面看NBA、买股票的各个同事。
代码:ObserverMain.java-入口类,Subject.java、ScretarySubject.java-发布者类,Observer.java-观察者抽象类,NBAObserver.java、StockObserver.java-具体观察者类。源码
ObserverMain.java-入口类
View Code
1 package publishSubscribe; 2 3 /** 4 * 观察者模式 5 */ 6 public class ObserverMain{ 7 public static void main(String[] args){ 8 ScretarySubject scretary = new ScretarySubject(); 9 scretary.setSubjectState("the boss is come back,work hardly!"); 10 NBAObserver nbaObserver = new NBAObserver("NBAObserver", scretary); 11 StockObserver stockObserver = new StockObserver("StockObserver", scretary); 12 13 scretary.attach(nbaObserver); 14 scretary.attach(stockObserver); 15 scretary.notifyObservers(); 16 } 17 18 }
Subject.java、ScretarySubject.java-发布者类
View Code
1 package publishSubscribe; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 /** 7 * 发布者 8 */ 9 public class Subject { 10 11 private List<Observer> observerList = new ArrayList<Observer>(); 12 13 public void attach(Observer ob) { 14 this.observerList.add(ob); 15 } 16 17 public void detach(Observer ob) { 18 this.observerList.remove(ob); 19 } 20 21 public void notifyObservers(){ 22 for(Observer o : observerList){ 23 o.update(); 24 } 25 } 26 }
View Code
1 package publishSubscribe; 2 3 public class ScretarySubject extends Subject { 4 private String subjectState; 5 6 public String getSubjectState() { 7 return subjectState; 8 } 9 10 public void setSubjectState(String subjectState) { 11 this.subjectState = subjectState; 12 } 13 14 }
Observer.java-观察者抽象类
View Code
1 package publishSubscribe; 2 3 /** 4 * 观察者 5 */ 6 public abstract class Observer{ 7 public abstract void update(); 8 }
NBAObserver.java、StockObserver.java-具体观察者类
View Code
1 package publishSubscribe; 2 3 public class NBAObserver extends Observer { 4 private String name; 5 private ScretarySubject sub; 6 7 public NBAObserver(String name, ScretarySubject sub) { 8 this.name = name; 9 this.sub = sub; 10 } 11 12 public String getName() { 13 return name; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 public ScretarySubject getSub() { 21 return sub; 22 } 23 24 public void setSub(ScretarySubject sub) { 25 this.sub = sub; 26 } 27 28 @Override 29 public void update() { 30 System.out.println( this.name + ":" + sub.getSubjectState()); 31 } 32 33 }
View Code
1 package publishSubscribe; 2 3 public class StockObserver extends Observer { 4 private String name; 5 private ScretarySubject sub; 6 7 public StockObserver(String name, ScretarySubject sub) { 8 this.name = name; 9 this.sub = sub; 10 } 11 12 public String getName() { 13 return name; 14 } 15 16 public void setName(String name) { 17 this.name = name; 18 } 19 20 public ScretarySubject getSub() { 21 return sub; 22 } 23 24 public void setSub(ScretarySubject sub) { 25 this.sub = sub; 26 } 27 28 @Override 29 public void update() { 30 System.out.println( this.name + ":" + sub.getSubjectState()); 31 } 32 33 }