觀察者模式主要用於處理對象間的一對多的關係,是一種對象行爲模式。該模式的實際應用場景比較容易確認,當一個對象狀態發生變化時,全部該對象的關注者均能收到狀態變化通知,以進行相應的處理。
本文但願經過簡單的介紹和分析,能讓讀者對觀察者模式有一個簡單直觀的認識和感知,以便在實際開發中根據須要靈活運用。html
創建對象間一對多的關聯關係,並能使一個對象的變化被全部關聯對象感知。異步
創建一套低耦合的消息觸發機制。ide
優勢:this
缺點:spa
下面是GoF介紹的典型的類觀察者模式的UML類圖:3d
Subject:code
抽象被觀察者,僅提供註冊和刪除觀察者對象的接口聲明。server
ConcreteSubject:htm
具體被觀察者對象,該對象中收集了全部須要被通知的觀察者,並能夠動態的增刪集合中的觀察者。當其狀態發生變化時會通知全部觀察者對象。對象
Observer:
抽象觀察者,爲全部觀察者定義得到通知的統一接口;
ConcreteObserver:
觀察者對象,其關注對象爲Subject,能接受Subject變化時發出的通知並更新自身狀態。
接下來先將上面的UML類圖轉換爲具體的代碼,而後在舉一個具體的例子來看一下其應用。
抽象被觀察者類:Subject
public interface Subject { public void setState(int state); public int getState(); public void attach(Observer obs); public void detach(Observer obs); public void notify(String msg); }
抽象觀察者類:Observer
public interface Observer { public void update(String msg); }
具體被觀察者類:ConcreteSubject
public class ConcreteSubject implements Subject { private List<Observer> observerList = new ArrayList<Observer>(); private int state; @Override public void setState(int state) { this.state = state; notify("new state: " + state); } @Override public int getState() { // TODO Auto-generated method stub return 0; } @Override public void attach(Observer obs) { // TODO Auto-generated method stub observerList.add(obs); } @Override public void detach(Observer obs) { // TODO Auto-generated method stub observerList.remove(obs); } @Override public void notify(String msg) { // TODO Auto-generated method stub for (Observer obs: observerList) { obs.update(msg); } } }
具體觀察者類:ConcreteObserver
public class ConcreteObserver implements Observer { @Override public void update(String msg) { // TODO Auto-generated method stub System.out.println("ConcreteObserver receive notify msg: " + msg); } }
演示:
public class Demo { public static void main(String[] args) { ConcreteObserver obs = new ConcreteObserver(); ConcreteSubject sub = new ConcreteSubject(); sub.attach(obs); sub.setState(666); sub.notify("just test subject notify function!"); } }
結果:
ConcreteObserver receive notify msg: new state: 666 ConcreteObserver receive notify msg: just test subject notify function!
咱們以一個更加實際的例子——商品價格的變更來體會一下觀察者模式的用途。
在網上購物的時候,商品通常都有一個價格變更通知,前提是咱們關注了該商品。
這裏咱們稍微變通一下,只有當關注的商品價格降低,且低於用戶指望購買價格的時候,纔會給用戶發送一條商品降價的短信通知。
下面分別定義每一個類:
產品抽象類:Product
public interface Product { public void setPrice(int price); public int getPrice(); public void follow(User user); public void unfollow(User user); public void notifyLowPrice(); }
用戶抽象類:User
public interface User { public boolean isExpectedPrice(int price); public void shortMSG(String msg); }
商品筆記本電腦:Laptop
public class Laptop implements Product { private List<User> followList = new ArrayList<User>(); private int curPrice; @Override public void setPrice(int price) { curPrice = price; System.out.println("set laptop price: " + price); notifyLowPrice(); } @Override public int getPrice() { return curPrice; } @Override public void follow(User user) { followList.add(user); } @Override public void unfollow(User user) { followList.remove(user); } @Override public void notifyLowPrice() { String msg = "" + curPrice; for (User user: followList) { if (user.isExpectedPrice(curPrice)) { user.shortMSG(msg); } } } }
關注筆記本電腦用戶類:LaptopBuyer
public class LaptopBuyer implements User { private int expectedPrice; private String userName; public LaptopBuyer(String userName, int expectedPrice) { this.userName = userName; this.expectedPrice = expectedPrice; } @Override public boolean isExpectedPrice(int curPrice) { // TODO Auto-generated method stub return curPrice <= expectedPrice; } @Override public void shortMSG(String msg) { // TODO Auto-generated method stub System.out.println("Your follow product have a low price: " + msg + " TO:" + userName); } }
演示:
public class Demo { public static void main(String[] args) { LaptopBuyer Alice = new LaptopBuyer("Alice", 6000); LaptopBuyer Jack = new LaptopBuyer("Jack", 6500); Laptop laptop = new Laptop(); laptop.follow(Alice); laptop.follow(Jack); laptop.setPrice(7000); laptop.setPrice(6500); laptop.setPrice(6000); laptop.unfollow(Jack); laptop.setPrice(5999); laptop.setPrice(6099); } }
結果:
set laptop price: 7000 set laptop price: 6500 Your follow product have a low price: 6500 TO:Jack set laptop price: 6000 Your follow product have a low price: 6000 TO:Alice Your follow product have a low price: 6000 TO:Jack set laptop price: 5999 Your follow product have a low price: 5999 TO:Alice set laptop price: 6099
上面的這個例子是一個可以很好地解釋觀察者模式的一個實際用途。
相比較與觀察者模式,咱們或許有許多獲取另一個對象狀態的方式,好比,常見的輪詢方式,或者僅僅在須要的時候去查一下對方的狀態等,不過觀察者模式有其特殊的用途,並且更加靈活。
該模式原理比較簡單直接,可是實際使用過程當中須要考慮一些細節問題:
上面這些都是實際使用時應該考慮的。考慮清楚這些細節才能更靈活的應用該模式解決實際問題。
參考:
GoF《Design Patterns: Elements of Reusable Object-Oriented Software》
https://www.runoob.com/design-pattern/observer-pattern.html