java委托

Posted itblock

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java委托相关的知识,希望对你有一定的参考价值。

上文讲过观察者模式,尽管已经用了依赖倒转原则,但是"抽象通知者"还是依赖"抽象观察者",也就是说万一没有了抽象观察者这样的接口,通知的功能就完不成了。另一方面,每个具体观察者不一定是要调用"更新"方法。此时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委托的主要内容,如果未能解决你的问题,请参考以下文章

创建委托 LdapAuthenticationProvider 而不使用 xml 配置,而不是通过 java 代码

java代码在片段活动中不起作用

java 代码片段【JAVA】

# Java 常用代码片段

# Java 常用代码片段

创建片段而不从 java 代码实例化它