大话设计模式-观察者模式-Java实现委托

定义一个事件类

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/** * @ClassName: Event * @Author: Leo * @Description: 事件类 * @Date: 2019/4/16 18:47 */
public class Event {
    /** * 被调用的对象 */
    private Object object;
    /** * 调用的方法名 */
    private String methodName;
    /** * 方法的参数 */
    private Object[] params;
    /** * 方法的参数类型 */
    private Class[] paramTypes;

    public Event(Object object, String methodName, Object... params) {
        this.object = object;
        this.methodName = methodName;
        this.params = params;
        initParamTypes(params);
    }

    /** * 初始化参数类型 * * @param params */
    private void initParamTypes(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() {
        try {
            Method method = object.getClass().getMethod(this.methodName, this.paramTypes);
            if (method == null) {
                return;
            }
            method.invoke(this.object, this.params);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}

管理事件的类

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

/** * @ClassName: EventHandler * @Author: Leo * @Description: 管理事件 * @Date: 2019/4/16 19:05 */
public class EventHandler {
    /** * 事件列表 */
    private List<Event> events;

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

    /** * 添加事件 * * @param object * @param methodName * @param params */
    public void addEvent(Object object, String methodName, Object... params) {
        events.add(new Event(object, methodName, params));
    }

    /** * 通知 */
    public void Notify() {
        for (Event event : events) {
            event.invoke();
        }
    }

}

抽象通知

/** * @ClassName: Notifier * @Author: Leo * @Description: * @Date: 2019/4/16 19:10 */
public abstract class Notifier {
    private EventHandler eventHandler = new EventHandler();

    public abstract void addListener(Object object, String methodName, Object... params);

    public abstract void Notify();

    public EventHandler getEventHandler() {
        return eventHandler;
    }

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

具体通知

/** * @ClassName: ConcreteNotifier * @Author: Leo * @Description: * @Date: 2019/4/16 19:12 */
public class ConcreteNotifier extends Notifier {
    @Override
    public void addListener(Object object, String methodName, Object... params) {
        this.getEventHandler().addEvent(object, methodName, params);
    }

    @Override
    public void Notify() {
        this.getEventHandler().Notify();
    }
}

监听

/** * @ClassName: WatchingTVListener * @Author: Leo * @Description: * @Date: 2019/4/16 19:24 */
public class WatchingTVListener {
    public WatchingTVListener() {
        System.out.println("WATCHING TV");
    }

    public void stopWatchingTV(String string) {
        System.out.println("STOP WATCHING " + string);
    }

}

测试

/** * @ClassName: Main * @Author: Leo * @Description: * @Date: 2019/4/16 19:24 */
public class Main {
    public static void main(String[] args) {
        Notifier notifier = new ConcreteNotifier();
        WatchingTVListener listener = new WatchingTVListener();
        notifier.addListener(listener, "stopWatchingTV", "方法参数");
        notifier.Notify();
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务