观察者模式定义了对象之间的1:N的依赖关系,当一个被观察对象的状态改变时,其N个观察依赖对象都会得到通知和更新,这种设计模式在系统设计中也被称为发布-订阅模式,广泛应用于系统解偶设计。
观察者设计模式中,多个观察者对象正在观察特定的主题对象,当主题对象内部状态发生变化时,观察者注册到主题对象中获得通知。
观察者对象可以自由的从订阅主题中注入与注销,达到系统松耦合的设计目的。
什么时候使用观察者模式
在我们进行系统设计的时候,如果存在多个对象状态变化依赖某一个特定主题对象内部状态变化的时候,则可以应用观察者模式。
首先需要明确我们的主题对象,其次我们将观察者注入并观察其内部状态变更,我们可以采用主动观察其状态变更行为或者被动接受行为变更时的通知,观察者当观察到变更时执行各自的行为。
观察者模式案例
在电子商务领域主要围绕线上商品交易打造,其中核心的模块是订单的生命周期,这个是观察者模式应用的典型场景,因为我们的系统服务需要在订单的各个状态变更时作出即时的内部操作或者用户服务等;如果我们应用观察者模式来订阅订单状态变更事件那么会为系统降低模块之间的代码耦合度。
观察者模式设计
- Order:观察者观察对象,案例围绕Order的内部状态变更来设计的发布订阅
- OrderUpdateManager:Order状态维护对象,扮演即时通知的角色
- Observer:观察者接口抽象,将其实现类对象注册与注销绑定至OrderUpdateManager
- DeliveredObserver:观察者实现,观察Order状态变更,作出相应的应对事件
Order.java
package com.iblog.pattern.observe;
public class Order {
public String id;
public Status status;
public enum Status {
RECEIVED,
AWAIT_PAYMENT,
PAYMENT_RECEIVED,
PROCESSING,
EXCEPTION,
SHIPPED,
DELIVERED
}
}
Observer.java
package com.iblog.pattern.observe;
public interface Observer {
void observe(Event event);
}
PaymentNotifyObserver.java
package com.iblog.pattern.observe;
public class PaymentNotifyObserver implements Observer {
@Override
public void observe(Event event) {
if (event.preStatus != Order.Status.RECEIVED) {
return;
}
System.out.println("notify the order owner to pay for order");
}
}
PaidFinalsObserver.java
package com.iblog.pattern.observe;
public class PaidFinalsObserver implements Observer {
@Override
public void observe(Event event) {
if (event.preStatus != Order.Status.AWAIT_PAYMENT) {
return;
}
System.out.println("user paid for order, notify finals to check bill");
}
}
ReceivedPaymentObserver.java
package com.iblog.pattern.observe;
public class ReceivedPaymentObserver implements Observer {
@Override
public void observe(Event event) {
if (event.preStatus != Order.Status.PAYMENT_RECEIVED) {
return;
}
System.out.println("received the payment of order, notify to process the order");
}
}
DeliveredObserver.java
package com.iblog.pattern.observe;
public class DeliveredObserver implements Observer {
@Override
public void observe(Event event) {
if (event.preStatus != Order.Status.SHIPPED) {
return;
}
System.out.println("order is delivered, notify use to check the package");
}
}
OrderUpdateManager.java
package com.iblog.pattern.observe;
import java.util.ArrayList;
import java.util.List;
public class OrderUpdateManager {
private final List observers = new ArrayList<>();
private final Notifier notifier;
public OrderUpdateManager() {
this.notifier = new Notifier(observers);
}
public void register(Observer observer) {
synchronized (observers) {
observers.add(observer);
}
}
public void unregister(Observer observer) {
synchronized (observers) {
observers.remove(observer);
}
}
public void awaitPaidOrder(Order order) {
notifier.notify(order, Order.Status.AWAIT_PAYMENT, "paid order");
}
public void receivedOrder(Order order) {
notifier.notify(order, Order.Status.RECEIVED, "await payment");
}
public void paidOrder(Order order) {
notifier.notify(order, Order.Status.PAYMENT_RECEIVED, "will process order");
}
public void delivered(Order order) {
notifier.notify(order, Order.Status.SHIPPED, "delivered");
}
}
Event.java
package com.iblog.pattern.observe;
public class Event {
public final Order order;
public final Order.Status preStatus;
public final String message;
public Event(Order order, Order.Status preStatus, String message) {
this.order = order;
this.preStatus = preStatus;
this.message = message;
}
}
Notifier.java
package com.iblog.pattern.observe;
import java.util.List;
public class Notifier {
private final List observers;
public Notifier(List observers) {
this.observers = observers;
}
public void notify(Order order, Order.Status preStatus, String subject) {
for (Observer o : observers) {
o.observe(new Event(order, preStatus, subject));
}
}
}
MockTest.java
package com.iblog.pattern.observe;
public class MockTest {
public static void main(String[] args) {
Order order = new Order();
order.id = "1";
order.status = Order.Status.RECEIVED;
OrderUpdateManager manager = new OrderUpdateManager();
Observer observer1 = new PaymentNotifyObserver();
Observer observer2 = new PaidFinalsObserver();
Observer observer3 = new ReceivedPaymentObserver();
Observer observer4 = new DeliveredObserver();
manager.register(observer1);
manager.register(observer2);
manager.register(observer3);
manager.register(observer4);
order.status = Order.Status.AWAIT_PAYMENT;
manager.receivedOrder(order);
order.status = Order.Status.PAYMENT_RECEIVED;
manager.awaitPaidOrder(order);
order.status = Order.Status.PROCESSING;
manager.paidOrder(order);
order.status = Order.Status.DELIVERED;
manager.delivered(order);
}
}
输出:
notify the order owner to pay for order
user paid for order, notify finals to check bill
received the payment of order, notify to process the order
order is delivered, notify use to check the package
Jdk中观察者模式
Observer Java Doc
Observable Java Doc