标签:观察者模式 new 视频 etc over return swing shm 设计模式
定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并被自动更新。
发布者/订阅者模式。
事件监听、swing中都有用到。
时间核心类
/** * 事件 * @author zzf * @date 2018/9/2 15:30. */ public class Event { //source private Object source; //target private Object target; //callback private Method callback; //触发事件 private String trigger; private long timestamp; public Event(Object target, Method callback) { this.target = target; this.callback = callback; } public long getTimestamp() { return timestamp; } public void setTimestamp(long timestamp) { this.timestamp = timestamp; } public Object getTarget() { return target; } public void setTarget(Object target) { this.target = target; } public Method getCallback() { return callback; } public void setCallback(Method callback) { this.callback = callback; } public Object getSource() { return source; } public void setSource(Object source) { this.source = source; } public String getTrigger() { return trigger; } Event setTrigger(String trigger) { this.trigger = trigger; return this; } @Override public String toString() { return "Event{" + "source=" + source + ", target=" + target + ", callback=" + callback + ", trigger=‘" + trigger + ‘\‘‘ + ‘}‘; } }
public class EventListener { //相当注册器 protected Map<Enum, Event> events = new HashMap<>(); public void addListener(Enum eventType, Object taget, Method callback) { //注册事件 //反射调用 events.put(eventType, new Event(taget, callback)); } private void trigger(Event e) { e.setSource(this); e.setTimestamp(System.currentTimeMillis()); try { e.getCallback().invoke(e.getTarget(), e); } catch (Exception e1) { } } protected void trigger(Enum call) { if (!this.events.containsKey(call)) { return; } trigger(this.events.get(call).setTrigger(call.toString())); } }
观察者
/** * 观察者 * @author zzf * @date 2018/9/2 15:29. */ public class Observer { public void advice(Event event) { System.out.println("====== 触发事件,打印日志====\n" + event); } }
被观察者
/** * 被观察者 * @author zzf * @date 2018/9/2 15:28. */ public class Subject extends EventListener { //通常采用动态里实现监控,避免代码侵入 public void add() { System.out.println("add()"); trigger(SubjectEventType.ON_ADD); } public void remove() { System.out.println("remove()"); trigger(SubjectEventType.ON_RMOVE); } }
public enum SubjectEventType {
ON_ADD,
ON_RMOVE,
ON_EDIT,
ON_QUERY;
}
test
public class ObserverTest { public static void main(String[] args) { try { //观察者和被观察者没有必然的联系 //注册的时候,才产生联系 Observer observer = new Observer(); Method advice = Observer.class.getMethod("advice", new Class[]{Event.class}); Subject subject = new Subject(); //添加监听 subject.addListener(SubjectEventType.ON_ADD, observer, advice); subject.add(); subject.remove(); } catch (Exception e) { } } }
》关联行为场景
》事件多级触发场景
》跨系统的消息交换场景,如消息队列的处理机制。
这是听咕泡学院视频的实战笔记。和书上的标准模式demo 相差还是蛮大的。
自己感觉能看懂,但是在实际中应用还是有点懵。
参考资料:
咕泡学院
标签:观察者模式 new 视频 etc over return swing shm 设计模式
原文地址:https://www.cnblogs.com/idea-persistence/p/9575560.html