监听者模式:(Swing监听器竟然是二叉树!!牛比!)

本文介绍了一个基于事件监听器的设计模式实现,包括不同类型的事件(如雨天事件、广播事件和警报事件)及其对应的监听器。通过具体示例展示了如何为特定事件注册监听器,并在事件发生时通知这些监听器。
package asf;
interface Listener {
}
abstract class Event {

	//public abstract void happen();

	public abstract int getId();
}

class RainEvent extends Event {
	private static final int id = 1;

	 /*public void happen() {
		// TODO Auto-generated method stub
		System.out.println("raining");
	}*/
	public String toString(){
		return "raining";	
	}
	
	public  int getId() {
		// TODO Auto-generated method stub
		return id;
	}
}

class AlarmEvent extends Event {
	private static final int id = 3;

	/*public void happen() {
		// TODO Auto-generated method stub
		System.out.println("Alarming");
	}*/
	public String toString(){
		return "Alarming";	
	}
	public int getId() {
		// TODO Auto-generated method stub
		return id;
	}
}

class BroadcastEvent extends Event {
	private static final int id = 2;
	public  int getId() {
		// TODO Auto-generated method stub
		return id;
	}
	/*public void happen() {
		// TODO Auto-generated method stub
		System.out.println("Broadcasting");
	}*/
	public String toString(){
		return "Broadcasting";	
	}
}

interface RainListener extends Listener {
	public void rain(Event e);
}

interface RadioListener extends Listener {
	public void broadcast(Event e);
}

interface ClockListener extends Listener {
	public void alarm(Event e);
}

class ListenerCaster implements RainListener, RadioListener, ClockListener {
	Listener l1;

	Listener l2;

	ListenerCaster(Listener l1, Listener l2) {
		this.l1 = l1;
		this.l2 = l2;
	}

	public void rain(Event e) {
		// TODO Auto-generated method stub
		if(l1!=null){
		((RainListener) l1).rain(e);
		}
		if(l2!=null){
			((RainListener) l2).rain(e);
		}
	}

	public void broadcast(Event e) {
		// TODO Auto-generated method stub\
		if(l1!=null){
		((RadioListener) l1).broadcast(e);}
		if(l2!=null){
			((RadioListener) l2).broadcast(e);}
	}

	public void alarm(Event e) {
		// TODO Auto-generated method stub
		if(l1!=null){
			((ClockListener) l1).alarm(e);}
			if(l2!=null){
				((ClockListener) l2).alarm(e);}
		
	}
}

class TheNotifiedObject {
	RainListener rl;

	RadioListener ral;

	ClockListener cl;

	public void addRainListener(RainListener rl) {
		this.rl = new ListenerCaster(this.rl, rl);
	}

	public void addRadioListener(RadioListener ral) {
		this.ral = new ListenerCaster(this.ral, ral);
	}

	public void addClockListener(ClockListener cl) {
		this.cl = new ListenerCaster(this.cl, cl);
	}

	protected void processEvent(Event e) {
		switch (e.getId()) {
		case 1:
			if(ral!=null){
			rl.rain(e);
			}
			break;
		case 2:
			if(ral!=null){
			ral.broadcast(e);}
			break;
		case 3:
			if(cl!=null){
			cl.alarm(e);}
			break;
		}
	}
}

public class TheNotifiedObjectA extends TheNotifiedObject {
	
	TheNotifiedObjectA() {
		addClockListener(new ClockListener() {
			public void alarm(Event e) {
				// TODO Auto-generated method stub
				System.out.println(e);
			}

		});
		addRainListener(new RainListener() {
			public void rain(Event e) {
				// TODO Auto-generated method stub
				System.out.println(e);
			}
		});
		addRadioListener(new RadioListener() {
			public void broadcast(Event e) {
				// TODO Auto-generated method stub
				System.out.println(e);
			}
		});
	}
	public static void main(String[] args){
		Event e1=new AlarmEvent();
		Event e2=new RainEvent();
		Event e3=new BroadcastEvent();
		TheNotifiedObjectA k=new TheNotifiedObjectA();
		k.processEvent(e1);
		k.processEvent(e2);
		k.processEvent(e3);	
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值