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

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);
		
	}

}

 

你可能感兴趣的:(监听器)