码迷,mamicode.com
首页 > 编程语言 > 详细

观察者模式和java委托

时间:2018-01-08 12:14:03      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:stat   tcl   函数   size   tac   info   管理   机制   params   

观察者模式与java委托

  • 所谓观察者模式,指的是在某种情况下需要同时触发一系列相关操作,它存在观察者和通知者两个概念,通知者持有观察者的抽象类的集合,可以调用所有观察者的特定方法以更新状态,同时观察者也可持有通知者的抽象对象以获取具体通知者的特有信息。

     技术分享图片

  • 观察者模式的不足:虽然观察者模式提取出了抽象观察者和通知者,让类与类之间不互相依赖,共同依赖于抽象接口,符合依赖倒转,但他任需要依赖于抽象接口,而且有些时候观察者无法继承抽象观察者(比如引用jar包)。
  • java委托机制与观察者模式:委托机制的实现不再需要观察者抽象类,观察者和通知者互不依赖。java利用反射即可实现,代码实例如下:

事件类

package com.suski.delegate;

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);//利用反射机制调用函数
    }
}

事件管理类

package com.suski.delegate;

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.suski.delegate;

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.suski.delegate;

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();
        }
    }
}

具体的观察者类,不再需要抽象观察者

package com.suski.delegate;

import java.util.Date;

public class WatchingTVListener {

    public WatchingTVListener()
    {
        System.out.println("watching TV");
    }
    
    public void stopWatchingTV(Date date) 
    {
        System.out.println("stop watching" + date);
    }
}

package com.suski.delegate;

import java.util.Date;

public class PlayingGameListener {
    public PlayingGameListener()
    {
        System.out.println("playing");
    }
    
    public void stopPlayingGame(Date date)
    {
        System.out.println("stop playing" + date);
    }
}

测试方法

package com.suski.delegate;

import java.util.Date;

public class Test {
    
    public static void main (String[] args)
    {
        Notifier goodNotifier = new ConcreteNotifier();
        
        PlayingGameListener playingGameListener = new PlayingGameListener();
        
        WatchingTVListener watchingTVListener = new WatchingTVListener();
        
        goodNotifier.addListener(playingGameListener, "stopPlayingGame", new Date());
        
        goodNotifier.addListener(watchingTVListener, "stopWatchingTV", new Date());
        
        goodNotifier.notifyX();
    }

}

 

观察者模式和java委托

标签:stat   tcl   函数   size   tac   info   管理   机制   params   

原文地址:https://www.cnblogs.com/wanshiming/p/8241876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!