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

java委托

时间:2019-01-06 21:59:12      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:老师   end   set   throws   handler   对象   import   参数类型   date   

上文讲过观察者模式,尽管已经用了依赖倒转原则,但是"抽象通知者"还是依赖"抽象观察者",也就是说万一没有了抽象观察者这样的接口,通知的功能就完不成了。另一方面,每个具体观察者不一定是要调用"更新"方法。此时java利用反射即可实现,具体代码如下所示:

Event

package com.jdk8.event.javaEvent;

import java.lang.reflect.Method;

public class Event {

    //要执行方法的对象
    private Object object;

    //要执行的方法名称
    private String methodName;

    //要执行方法的参数
    private Object[] params;

    //要执行方法的参数类型
    private Class[] paramTypes;

    public Event(){

    }

    public Event(Object object,String methodName,Object...args){
        this.object = object;
        this.methodName = methodName;
        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 = null;
        method = object.getClass().getMethod(this.getMethodName(),this.getParamTypes());
        method.invoke(this.getObject(),this.getParams());
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public String getMethodName() {
        return methodName;
    }

    public void setMethodName(String methodName) {
        this.methodName = methodName;
    }

    public Object[] getParams() {
        return params;
    }

    public void setParams(Object[] params) {
        this.params = params;
    }

    public Class[] getParamTypes() {
        return paramTypes;
    }

    public void setParamTypes(Class[] paramTypes) {
        this.paramTypes = paramTypes;
    }
}

EventHandler

package com.jdk8.event.javaEvent;


import java.util.ArrayList;
import java.util.List;

public class EventHandler {

    private List<Event> objects;

    public EventHandler(){
        objects = new ArrayList<>();
    }

    //添加某个对象要执行的事件及需要的参数
    public void addEvent(Object object,String methodName,Object...args){
        objects.add(new Event(object,methodName,args));
    }

    //通知所有的对象执行制定的事件
    public void notifyX()  {
        for(Event e : objects){
            try {
                e.invoke();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}

Notifier抽象类

package com.jdk8.event.javaEvent;


public abstract class Notifier {
    private EventHandler eventHandler = new EventHandler();

    public EventHandler getEnventHandler(){
        return eventHandler;
    }

    public void setEventHandler(EventHandler eventHandler){
        this.eventHandler = eventHandler;
    }

    //增加需要放哨的学生
    public abstract void addListener(Object object,String methodname,Object...args);

    //告诉所有要帮忙放哨的学生:老师来了
    public abstract void notifyX();
}

GoodNotifier

package com.jdk8.event.javaEvent;

public class GoodNotifier extends Notifier {

    @Override
    public void addListener(Object object, String methodname, Object... args) {
        System.out.println("有新的同学委托尽职尽责的放哨人!");
        this.getEnventHandler().addEvent(object,methodname,args);
    }

    @Override
    public void notifyX() {
        System.out.println("尽职尽责的放哨人告诉所有需要帮忙的同学:老师来了!");
        try{
            this.getEnventHandler().notifyX();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

PlayingGameListener

package com.jdk8.event.javaEvent;

import java.util.Date;

public class PlayingGameListener {
    public PlayingGameListener(){
        System.out.println("我正在玩游戏,开始时间" + new Date());
    }

    public void stopPlayingGame(Date date,String name){
        System.out.println("老师来了,快回到座位上,结束时间"+date + " " + name);
    }
}

WatchingTVListener

package com.jdk8.event.javaEvent;

import java.util.Date;

public class WatchingTVListener {
    public WatchingTVListener(){
        System.out.println("我正在看电视,开始时间" + new Date());
    }

    public void stopWatchingTV(Date date){
        System.out.println("老师来了,快回到座位上,结束时间"+date);
    }
}

TestMain

package com.jdk8.event.javaEvent;

import java.util.Date;

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

        goodNotifier.notifyX();
    }
}

java委托

标签:老师   end   set   throws   handler   对象   import   参数类型   date   

原文地址:https://www.cnblogs.com/ITBlock/p/10217685.html

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