首页 > 代码库 > 《大话设计模式》——读后感 (6)老板回来了,我不知道?——观察者模式之事件委托(2)

《大话设计模式》——读后感 (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的事件订阅模式,就是充分的发挥的观察者模式的作用,有兴趣的可以看看

 

 

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