Event Driver设计模式

一、EDA是一种实现组件之间松耦合、易扩展的架构方式,EDA设计模式包含的组件:
1、Events:需要被处理的数据
2、Event Handlers:处理Events的方法
3、Event Loop:维护Events与Event Handlers之间的交互流程
二、EDA框架的基础接口设计:
1、Message:相当于上面Event
/**

  • Event可以被称为Message
    */
    public interface Message {

    /**

    • 返回Message的类型
      */
      Class< extends Message> getType();
      }

2、Channel:相当于上面的Event Handlers
/**

  • 主要是接受来自于Event Loop分配的消息
    */
    public interface Channel {

    /**

    • dispatch方法用于负责Message的调度
      */
      void dispatch(E message);
      }

3、DynamicRouter:相当于上面的Event Loop
/**

  • 类似Event loop,主要作用是帮助Message找到合适的Channel

  • @param
    /
    public interface DynamicRouter {
    /
    *

    • 针对每一种Message类型注册相关的Channel,只有找到合适的Channel,该Message才会被处理
      */
      void registerChannel(Class< extends E> messageType, Channel< extends E> channel);

    /**

    • 为相应的Channel分配Message
      */
      void dispatcher(E message);
      }

4、Event:实现Message,作为事件的基类
public class Event implements Message{
@Override
public Class< extends Message> getType() {
return getClass();
}
}

三、同步方式处理事件
1、SyncChannel:同步处理事件的Channel
public class SyncChannel implements Channel {

@Override
public void dispatch(Event message) {
    System.out.println("处理数据了");
}

}

2、SyncEventDispatcher:注册并分发事件
public class SyncEventDispatcher implements DynamicRouter {

/**
 * 创建一个线程安全的容器
 */
private final Map, AsyncChannel> routerTable;

public SyncEventDispatcher(){
    this.routerTable = new HashMap<>();
}

@Override
public void registerChannel(Class messageType, Channel channel) {
    if (!(channel instanceof AsyncChannel)){
        throw new IllegalArgumentException("The Channel must be AsyncChannel Type.");
    }
    this.routerTable.put(messageType, (AsyncChannel)channel);
}

@Override
public void dispatcher(Event message) {
    if (this.routerTable.containsKey(message)){
        this.routerTable.get(message.getType()).dispatch(message);
    }else{
        throw new IllegalArgumentException("Can't match the channel for [" + message.getType() + "] type.");
    }
}

}

三、异步方式处理事件
1、AsyncChannel:异步多线程的方式处理事件
public abstract class AsyncChannel implements Channel {

/**
 * 使用线程池的方式处理Message
 */
private final ExecutorService executorService;

/**
 * 使用系统自定义的executorService
 */
public AsyncChannel(){
    this(Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2));
}

/**
 * 用户自定义的executorService
 * @param executorService
 */
public AsyncChannel(ExecutorService executorService){
    this.executorService = executorService;
}

/**
 * 重写dispatch方法,并且使用final修饰,防止子类重写
 * @param message
 */
@Override
public void dispatch(Event message) {
    executorService.submit(() -> this.handle(message));
}

/**
 * 提供抽象方法,子类进行具体的实现
 */
protected abstract void handle(Event message);

/**
 * 关闭线程池
 */
void stop(){
    if (null != executorService && !executorService.isShutdown()){
        executorService.shutdown();
    }
}

}

2、AsyncEventDispatcher:异步的注册并分发事件
public class AsyncEventDispatcher implements DynamicRouter {

/**
 * 创建一个线程安全的容器
 */
private final Map, AsyncChannel> routerTable;

public AsyncEventDispatcher(){
    this.routerTable = new ConcurrentHashMap<>();
}

@Override
public void registerChannel(Class messageType, Channel channel) {
    if (!(channel instanceof AsyncChannel)){
        throw new IllegalArgumentException("The Channel must be AsyncChannel Type.");
    }
    this.routerTable.put(messageType, (AsyncChannel)channel);
}

@Override
public void dispatcher(Event message) {
    if (this.routerTable.containsKey(message)){
        this.routerTable.get(message.getType()).dispatch(message);
    }else{
        throw new IllegalArgumentException("Can't match the channel for [" + message.getType() + "] type.");
    }
}

/**
 * 关闭所有的Channel以释放资源
 */
public void shutdown(){
    this.routerTable.values().forEach(AsyncChannel::stop);
}

}

你可能感兴趣的:(java,后端,java,后端)