关闭

模拟“事件监听器”(二)

257人阅读 评论(0) 收藏 举报

1、定义一个事件类

package hb.event;


public class Event {
	
	private EventType type;
    private int nodeID;
    private boolean processed;

    public Event(EventType type, int oldNodeID) {
        this.type = type;
        this.nodeID = oldNodeID;
    }

    public EventType getType() {
        return type;
    }

    public int getNodeID() {
		return nodeID;
	}

	public void setNodeID(int nodeID) {
		this.nodeID = nodeID;
	}

	public void setType(EventType type) {
		this.type = type;
	}

	public boolean isProcessed() {
        return processed;
    }

    public void setProcessed(boolean processed) {
        this.processed = processed;
    }

    @Override
	public String toString() {
        return super.toString() + " type: " + type;
    }
    
    public static enum EventType {

        /**
         * This JVM joined a cluster.
         */
        receive,

        /**
         * This JVM is no longer part of the cluster.
         */
        send,

    }
}

 

2、定义一个监听接口

package hb.event;

public interface EventListener {
	
	public void action(Event event);
	
}

 

3、添加两个监听接口实现类

package hb.event;

public class ReceiveEventListener implements EventListener {

	private Event event;
	
	@Override
	public void action(Event event) {
		// TODO Auto-generated method stub
		System.out.println("ReceiveEventListener");
	}
	
	public ReceiveEventListener(Event event){
		this.event = event;
	}
	
	public ReceiveEventListener(){
	}

}

 

package hb.event;

public class SendEventListener implements EventListener {

	private Event event;

	@Override
	public void action(Event event) {
		// TODO Auto-generated method stub
		System.out.println("SendEventListener");
	}

	public SendEventListener(Event event) {
		this.event = event;
	}
	
	public SendEventListener() {
	}
}

 

4、定义一个事件源(即监听事件的目标)

package hb.event;

import java.util.HashMap;
import java.util.Map;

public class Resource {
	
	
	Map<Event.EventType,EventListener> map = new HashMap<Event.EventType,EventListener>();
	
	public void addListener(Event.EventType eventType,EventListener eventListener){
		map.put(eventType, eventListener);
	}
	
	public void removeListener(Event.EventType eventType){
		map.remove(eventType);
	}
	
	public void sendAction(Event event){
		EventListener eventListener = (EventListener)map.get(Event.EventType.send);
		if(eventListener!=null){
			eventListener.action(event);
		}
	}
	
	public void receiveAction(Event event){
		EventListener eventListener = (EventListener)map.get(Event.EventType.receive);
		if(eventListener!=null){
			eventListener.action(event);
		}
	}
	
	
}

 

5、测试类

package hb.event;

public class EventTest {

	public static void main(String[] args) {
		
		Resource resource = new Resource();
		
		ReceiveEventListener receiveListener = new ReceiveEventListener();
		SendEventListener sendListener = new SendEventListener();
		
		
		resource.addListener(Event.EventType.receive, sendListener);
		resource.addListener(Event.EventType.send, sendListener);
		
		
		Event sendevent = new Event(Event.EventType.send,1);
		Event receiveevent = new Event(Event.EventType.receive,2);
		
		resource.sendAction(sendevent);
		resource.receiveAction(receiveevent);
		
	}

}

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:309044次
    • 积分:8705
    • 等级:
    • 排名:第2263名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论