参考文章
定义
观察者模式主要用于处理对象间的一对多的关系,是一种对象行为模式。
该模式的实际应用场景比较容易确认,当一个对象状态发生变化时,所有该对象的关注者均能收到状态变化通知,以进行相应的处理。
优缺点
被观察者和观察者之间是抽象耦合的;
耦合度较低,两者之间的关联仅仅在于消息的通知;
被观察者无需关心他的观察者;
支持广播通信;
观察者只知道被观察对象发生了变化,但不知变化的过程和缘由;
观察者同时也可能是被观察者,消息传递的链路可能会过长,完成所有通知花费时间较多;
如果观察者和被观察者之间产生循环依赖,或者消息传递链路形成闭环,会导致无限循环;
应用场景
需要在系统中建立一个单项广播的触发机制;
系统中某个对象的行为会影响若干其他对象;
对象之间的关联关系可以在运行时动态的建立与撤销;
对象之间的关联关系呈现出一种树状结构;
原理
抽象被观察者,仅提供注册和删除观察者对象的接口声明。
具体被观察者对象,该对象中收集了所有需要被通知的观察者,并可以动态的增删集合中的观察者。当其状态发生变化时会通知所有观察者对象。
抽象观察者,为所有观察者定义获得通知的统一接口;
观察者对象,其关注对象为Subject,能接受Subject变化时发出的通知并更新自身状态。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201117175516217.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY4NzE2Ng==,size_16,color_FFFFFF,t_70#pic_center)
实现
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);
}
public interface Observer {
public void update(String msg);
}
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() {
return 0;
}
@Override
public void attach(Observer obs) {
observerList.add(obs);
}
@Override
public void detach(Observer obs) {
observerList.remove(obs);
}
@Override
public void notify(String msg) {
for (Observer obs: observerList) {
obs.update(msg);
}
}
}
public class ConcreteObserver implements Observer {
@Override
public void update(String msg) {
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!
实例
我们以一个更加实际的例子——商品价格的变动来体会一下观察者模式的用途。
在网上购物的时候,商品一般都有一个价格变动通知,前提是我们关注了该商品。
这里我们稍微变通一下,只有当关注的商品价格下降,且低于用户期望购买价格的时候,才会给用户发送一条商品降价的短信通知。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201117175724296.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY4NzE2Ng==,size_16,color_FFFFFF,t_70#pic_center)
public interface Product {
void setPrice(int price);
int getPrice();
void follow(User user);
void unfollow(User user);
void notifyLowPrice();
}
public interface User {
boolean isExpectedPrice(int price);
void shortMSG(String msg);
}
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);
}
}
}
}
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) {
return curPrice <= expectedPrice;
}
@Override
public void shortMSG(String msg) {
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