在看源码的时候我们经常会看到Observer、Listener、Hook、callback等单词,不要被这些词所迷惑,实际上说的都是观察者模式,
观察模式解决方案图如下:
具体代码:
import java.util.ArrayList; import java.util.List; /** * 有很多时候,观察者需要根据事件的具体情况来进行处理 * 大多数时候,我们处理事件的时候,需要事件源对象 * 事件也可以形成继承体系 */ class Child { private boolean cry = false; private List<Observer> observers = new ArrayList<>(); { observers.add(new Dad()); observers.add(new Mum()); observers.add(new Dog());
observers.add(e -> System.out.println("ppp"));
//hook callback function } public boolean isCry() { return cry; } public void wakeUp() { cry = true; wakeUpEvent event = new wakeUpEvent(System.currentTimeMillis(), "bed", this); for(Observer o : observers) { o.actionOnWakeUp(event); } } } abstract class Event<T> { abstract T getSource(); } class wakeUpEvent extends Event<Child>{ long timestamp; String loc; Child source; public wakeUpEvent(long timestamp, String loc, Child source) { this.timestamp = timestamp; this.loc = loc; this.source = source; } @Override Child getSource() { return source; } } interface Observer { void actionOnWakeUp(wakeUpEvent event); } class Dad implements Observer { public void feed() { System.out.println("dad feeding..."); } @Override public void actionOnWakeUp(wakeUpEvent event) { feed(); } } class Mum implements Observer { public void hug() { System.out.println("mum hugging..."); } @Override public void actionOnWakeUp(wakeUpEvent event) { hug(); } } class Dog implements Observer { public void wang() { System.out.println("dog wang..."); } @Override public void actionOnWakeUp(wakeUpEvent event) { wang(); } } public class Main { public static void main(String[] args) { Child c = new Child(); //do sth c.wakeUp(); } }