• 【行为型模式】《大话设计模式》——读后感 (6)老板回来了,我不知道?——观察者模式之事件委托(2)


    什么都不说,直接上代码,自己去理解,要结合动态代理看

    Event:

    package com.sjmx.observer.application;
    
    import java.lang.reflect.Method;
    
    public class Event { 
        
        private Object object;  
          
        private String methodName;  
          
        private Object[] params;  
          
        private Class[] paramTypes;  
          
        public Event(Object object,String method,Object...args)  
        {  
            this.object = object;  
            this.methodName = method;  
            this.params = args;  
            contractParamTypes(this.params);  
        }  
          
        private void contractParamTypes(Object[] params)  
        {  
            this.paramTypes = new Class[params.length];  
            for (int i=0;i<params.length;i++)  
            {  
                this.paramTypes[i] = params[i].getClass();  
            }  
        }  
      
        public void invoke() throws Exception  
        {  
            Method method = object.getClass().getMethod(this.methodName, this.paramTypes);//判断是否存在这个函数  
            if (null == method)  
            {  
                return;  
            }  
            method.invoke(this.object, this.params);//利用反射机制调用函数  
        }  
    }  
    EventHandler :
    package com.sjmx.observer.application;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class EventHandler {
        
        private List<Event> objects;  
        
        public EventHandler()  
        {  
            objects = new ArrayList<Event>();  
        }  
          
        public void addEvent(Object object, String methodName, Object...args)  
        {  
            objects.add(new Event(object, methodName, args));  
        }  
          
        public void notifyX() throws Exception  
        {  
            for (Event event : objects)  
            {  
                event.invoke();  
            }  
        }  
        
    }

    主题(通知)接口:

    package com.sjmx.observer.application;
    
    public abstract class Notifier {
        
          private EventHandler eventHandler = new EventHandler();  
          
            public EventHandler getEventHandler()  
            {  
                return eventHandler;  
            }  
              
            public void setEventHandler(EventHandler eventHandler)  
            {  
                this.eventHandler = eventHandler;  
            }  
              
            public abstract void addListener(Object object,String methodName, Object...args);  
              
            public abstract void notifyX();  
            
    }

    具体通知实体:

    package com.sjmx.observer.application;
    
    public class ConcreteNotifier extends Notifier {
    
        @Override
        public void addListener(Object object, String methodName, Object... args) {
              this.getEventHandler().addEvent(object, methodName, args); 
        }
    
        @Override
        public void notifyX() {
            try {  
                this.getEventHandler().notifyX();  
            } catch (Exception e) {  
                // TODO: handle exception  
                e.printStackTrace();  
            }  
        }
    
    }

    具体观察者:

    1、不好好工作,在扯淡:

    package com.sjmx.observer.application;
    
    import java.util.Date;
    
    public class TalkingListener {
        
        public TalkingListener() 
        {
            System.out.println("working no,Talking");
        }
    
        public void stopTalking(Date date) 
        {
            System.out.println("stop talking:" + date);
        }
    }

    2、上班期间看NBA

    package com.sjmx.observer.application;
    
    import java.util.Date;
    
    public class WatchingNBAListener {
    
        public WatchingNBAListener() 
        {
            System.out.println("watching NBA");
        }
    
        public void stopWatchingNBA(Date date) 
        {
            System.out.println("stop watching:" + date);
        }
    }

    3、玩股票

    package com.sjmx.observer.application;
    
    import java.util.Date;
    
    public class WatchingStockListener {
        
        public WatchingStockListener() 
        {
            System.out.println("watchingStock");
        }
    
        public void stopWatchingStock(Date date) 
        {
            System.out.println("stop watchingStock:" + date);
        }
        
    }

    客户端:

    package com.sjmx.observer.application;
    
    import java.util.Date;
    
    public class Client {
        
          public static void main (String[] args)  
            {  
                Notifier goodNotifier = new ConcreteNotifier();  
                
                //看NBA
                WatchingNBAListener watchingNBAListener = new WatchingNBAListener();  
                //看股票
                WatchingStockListener watchingStockListener = new WatchingStockListener(); 
                //不好好工作,在聊天
                TalkingListener talkingListener = new TalkingListener();
                  
                goodNotifier.addListener(watchingNBAListener, "stopWatchingNBA", new Date());  
                goodNotifier.addListener(watchingStockListener, "stopWatchingStock", new Date());  
                goodNotifier.addListener(talkingListener, "stopTalking", new Date());
                  
                goodNotifier.notifyX();  
            }  
          
    }

    zookeeper的事件订阅模式,就是充分的发挥的观察者模式的作用,有兴趣的可以看看

     
  • 相关阅读:
    深度卷积网络中如何进行上采样?
    权重衰减(weight decay), L2正则
    python中的super().__init__()
    随机变量的间独立性及其传播
    卡尔曼滤波、扩展卡尔曼滤波、无迹卡尔曼滤波以及粒子滤波原理
    贝叶斯推断中的后验概率、似然函数、先验概率以及边际似然定义(转)
    详解最大似然估计(MLE)、最大后验概率估计(MAP),以及贝叶斯公式的理解(转)
    python告警发微信
    python模块operator操作符函数
    Django之CSRF验证。
  • 原文地址:https://www.cnblogs.com/chen1-kerr/p/7058674.html
Copyright © 2020-2023  润新知