设计模式--观察者模式 -- 监听器模式

为方便方便阅读,使用一个Java文件中创建多个class形式!

本文利用的鼠标的监听器事件实现了观察者模式。 

类图如下:

 

设计模式--观察者模式 -- 监听器模式_第1张图片

Event对象以及监控器(**Listener)对象

package com.example.demo.design.model.observe;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author fuwenshen
 * @description  监听的事件
 * @date 2020/3/26
 */
public class Event {

    //通知目标
    private Object target;
    //回调方法
    private Method callback;

    public Event(Object target, Method callback) {
        this.target = target;
        this.callback = callback;
    }
    
    public Object getTarget() {
        return target;
    }

    public Method getCallback() {
        return callback;
    }
    
}



/**
 * 事件的注册和监听
 */
class EventLisenter {

    //Map相当于是一个注册器
    protected Map events = new ConcurrentHashMap<>();

    public void addLisenter(Enum eventType, Object target,String methodName ){
        //注册事件
        //用反射调用这个方法
        try {
            Method method = target.getClass().getDeclaredMethod(methodName,new Class[]{Event.class});
            events.put(eventType,new Event(target,method));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    /**
     * 触发方法
     * @param call
     */
    protected void trigger(Enum call) {
        if (!this.events.containsKey(call)) {
            return;
        }
        trigger(this.events.get(call));
    }


    private void trigger(Event e) {

        try {
            e.getCallback().invoke(e.getTarget(), e);
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

}

 鼠标对象

package com.example.demo.design.model.observe;

/**
 * @author fuwenshen
 * @description  鼠标对象 (被观察者)
 * @date 2020/3/26
 */
public class Mouse extends EventLisenter {

    public void onClick(){
        System.out.println( "单击鼠标!");
        //点击同时去触发监听器,(通知观察者)
        trigger(MouseEnventType.ON_CLICK);

    }

    public void onDoubleClick(){
        System.out.println( "双击鼠标!");
        trigger(MouseEnventType.ON_DOUBLE_CLICK);
    }

    public void onRightClick(){
        System.out.println( "单击鼠标右键!");
        trigger(MouseEnventType.ON_RIGHT_CLICK);
    }
}


/***
 * 鼠标点击事件
 */
enum MouseEnventType{

    ON_CLICK,
    ON_DOUBLE_CLICK,
    ON_RIGHT_CLICK,

    ;
}

观察者对象(CallBack对象)

package com.example.demo.design.model.observe;

/**
 * @author fuwenshen
 * @description  监听到鼠标 事件 做出反馈,  (观察者)
 * @date 2020/3/26
 */
public interface MouceEvnetCallBack {

     String ON_CLICK ="onClick";
     String ON_DOUBLE_CLICK ="onDoubleClick";
     String ON_RIGHT_CLICK ="onRightClick";
    

    public void onClick(Event e);

    public void onDoubleClick(Event e);

    public void onRightClick(Event e);

}


/**
 * 自定义实现类
 */
class MyMouceEvnetCallBack implements MouceEvnetCallBack{

    @Override
    public void onClick(Event e){
        System.out.println( "监听到 ---单击鼠标事件!");
        // 可以根据event 处理对应的逻辑
    }

    @Override
    public void onDoubleClick(Event e){
        System.out.println( "监听到 ---双击鼠标事件!");

    }

    @Override
    public void onRightClick(Event e){
        System.out.println( "监听到 ---单击鼠标右键事件!");
    }
}
 

ObserveTest 测试类

package com.example.demo.design.model.observe;

/**
 * @author fuwenshen
 * @description TODO
 * @date 2020/3/26
 */
public class ObserveTest {

    public static void main(String[] args) {


        Mouse mouse = new Mouse();

        // (观察者) 监测反馈
        MouceEvnetCallBack observe = new MyMouceEvnetCallBack();

        mouse.addLisenter(MouseEnventType.ON_CLICK, observe, MouceEvnetCallBack.ON_CLICK);
        mouse.addLisenter(MouseEnventType.ON_DOUBLE_CLICK, observe, MouceEvnetCallBack.ON_DOUBLE_CLICK);
        mouse.addLisenter(MouseEnventType.ON_RIGHT_CLICK, observe, MouceEvnetCallBack.ON_RIGHT_CLICK);

        mouse.onClick();

        mouse.onDoubleClick();
        mouse.onRightClick();


    }
}

输出结果:

单击鼠标!
监听到 ---单击鼠标事件!
双击鼠标!
监听到 ---双击鼠标事件!
单击鼠标右键!
监听到 ---单击鼠标右键事件!

你可能感兴趣的:(设计模式)