天天看点

事件触发器 观察者模式 + 中介者模式

事件触发器 观察者模式 + 中介者模式
import java.util.Vector;// 事件处理着public abstract  class EventCustomer {    // 用于容纳消费者处理级别的容器
    private Vector customType = new Vector();    // 消费者声明处理哪一类事件
    public EventCustomer(EventCustomType type){
        addCustomType(type);
    }    // 保存消费者消费事件的级别
    public void addCustomType(EventCustomType type){
        customType.add(type);
    }    // 得到处理能力
    public Vector getCustomType(){        return customType;
    }    // 对事件消费
    public abstract void exec(ProductEvent event);
}           
// 克隆接口public interface Cloneable {    public Cloneable clone();
}           
// 平民处理事件public class Commoner extends EventCustomer{    // 定义处理级别
    public Commoner(){
        super(EventCustomType.NEW);
    }    // 处理
    @Override    public void exec(ProductEvent event){        // 事件源头
        Product product = event.getSource();        // 事件类型
        ProductEventType type = event.getEventType();        // 处理事件
        System.out.println(product.getName() + "平民处理事件");
    }
}           
import java.util.Vector;// 事件处理着public abstract  class EventCustomer {    // 用于容纳消费者处理级别的容器
    private Vector customType = new Vector();    // 消费者声明处理哪一类事件
    public EventCustomer(EventCustomType type){
        addCustomType(type);
    }    // 保存消费者消费事件的级别
    public void addCustomType(EventCustomType type){
        customType.add(type);
    }    // 得到处理能力
    public Vector getCustomType(){        return customType;
    }    // 对事件消费
    public abstract void exec(ProductEvent event);
}           
// 事件枚举类public enum EventCustomType {    // 建立事件
    NEW(1),    // 删除事件
    DEl(2),    // 修改事件
    EDIT(3),    // 克隆事件
    CLONE(4);    private int value = 0;    private EventCustomType(int value){        this.value = value;
    }    public int getValue(){        return value;
    }
}           
import java.util.Observable;import java.util.Observer;import java.util.Vector;// 用于事件观察者// 同时也承担对事件的分发// 由于事件分发只能有一个所以必须使用单例模式,对事件进行管理public class EventDispatch implements Observer {    // 单例模式
    private final static EventDispatch dispatch = new EventDispatch();    // 产生事件消费者容器
    // 一般情况是直接xml配置文件,或者IOC容器的配置规则,在框架启动的时候,加载进入内存
    private Vector customer = new Vector();    // 不允许生成新实例
    private EventDispatch(){

    }    // 获得对象
    public static EventDispatch getEventDispatch(){        return dispatch;
    }    // 事件触发
    // 第一个参数是事件,第二个参数是谁触发了事件

    // 注册事件处理着
    public void registerCustomer(EventCustomer customer){        this.customer.add(customer);
    }    @Override
    public void update(Observable o, Object arg) {        // 事件源头
        Product product = (Product)arg;        // 事件
        ProductEvent event = (ProductEvent)o;        // 处理着进行处理,此处是中介者模式
        for(Object e:customer){            for(Object t:((EventCustomer)e).getCustomType()){                // 如果消费级别,和ProductEvent中保存的级别相同,则会进行处理,中介者模式
                if(((EventCustomType)t).getValue() == event.getEventType().getValue()){                    // 执行
                    ((EventCustomer)e).exec(event);
                }
            }
        }
    }
}           
// 贵族处理事件public class Nobleman extends EventCustomer{    // 定义处理级别
    public Nobleman(){
        super(EventCustomType.EDIT);
        super.addCustomType(EventCustomType.CLONE);
    }    // 处理
    @Override    public void exec(ProductEvent event){        // 事件源头
        Product product = event.getSource();        // 事件类型
        ProductEventType type = event.getEventType();        if(type.getValue() == EventCustomType.CLONE.getValue()){
            System.out.println("克隆");
        }else{
            System.out.println("修改");
        }
    }
}           
// 产品public class Product implements  Cloneable{    // 产品名称
    private String name;    // 属性
    private boolean canChanged = false;    // 新产品
    public Product(ProductManager manager, String _name){        // 建立产品
        if(manager.isCreateProduct()){            this.canChanged = true;            this.name = _name;
        }
    }    public String getName(){        return name;
    }    public void setName(String name){        // 允许修改
        if(canChanged){            this.name = name;
        }
    }    @Override
    public Product clone() {
        Product product = null;        try{
            product = (Product)super.clone();
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }        return null;
    }
}           
import org.w3c.dom.events.EventListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.lang.annotation.Annotation;import java.util.Observable;// 产品事件,用于产生事件public class ProductEvent extends Observable {    // 事件
    private Product source;    // 类型
    private ProductEventType type;    // 触发
    public ProductEvent(Product product){        this(product, ProductEventType.NEW_PRODUCT);
    }    public ProductEvent(Product product, ProductEventType type){        // 产生事件触发的产品
        this.source = product;        // 从配置类中读取的标号
        this.type = type;        // 触发事件
        notifyEventDispatch();
    }    // 获得产品
    public Product getSource(){        return source;
    }    // 获得类型
    public ProductEventType getEventType(){        return this.type;
    }    // 通知事件处理中心
    private void notifyEventDispatch(){        // 把观察者加入容器中
        super.addObserver(EventDispatch.getEventDispatch());        // 立马返回结果,而不是把结果缓存
        super.setChanged();        // 通知观察者,把发生的事件传入
        super.notifyObservers(source);
    }
}           
public enum ProductEventType {    // 新建产品
    NEW_PRODUCT(1),    // 删除
    DEL_PRODUCT(2),    // 修改
    EDIT_PRODUCT(3),    // clone
    CLONE_PRODUCT(4);    private int value = 0;    private ProductEventType(int value){        this.value = value;
    }    public int getValue(){        return this.value;
    }
}           
// 工厂类public class ProductManager {    // 创建产品
    private boolean isPermittedCreate = false;    // 建立产品
    public Product createProduct(String name){        // 创建同意
        isPermittedCreate = true;
        Product product = new Product(this, name);        // 同时产生一个创建事件
        // 并且该事件将会注册到观察着中心,并把注册的观察者全部进行通知
        new ProductEvent(product, ProductEventType.NEW_PRODUCT);        return product;
    }    // 销毁产品
    public void abandonProduct(Product product){        // 此时将会产生删除事件
        new ProductEvent(product,ProductEventType.DEL_PRODUCT);
        product = null;
    }    // 修改产品
    public void editProduct(Product product, String name){        // 修改产品
        // 产生一个修改事件
        new ProductEvent(product, ProductEventType.EDIT_PRODUCT);
        product.setName(name);
    }    // 查询是否能创建产品
    public boolean isCreateProduct(){        return this.isPermittedCreate;
    }    // 克隆产品
    public Product clone(Product product){        // 产生一个克隆事件
        new ProductEvent(product,ProductEventType.CLONE_PRODUCT);        return product.clone();
    }
}