看了AS3中的事件机制,模仿写了个监听器(观察者)模式

现在已经加入了flash的行列中了,但是还是不舍得抛掉JAVA,偶尔还是会写写java代码;

感觉要比设计模式中的简单的观察者模式有所改进,也有诸多不足,还望多指点指教。

谢谢~~~~~

 

不想说太多的解释(正在上班中... .),先画个UML类图看看吧,很丑陋(没有工具,就直接用windows自带的画图工具了):

 

 

 

画个图累死了~~~

 

下面直接看代码吧:(还是以网上的"模拟公交车上的打卡机打卡"为例)

 

 

 

//----------------------模拟公交车上的打卡机打卡-------------CardMachine.java

import java.util.Date;

/**
 * 发送者
 * 
 * @author junlas
 * 
 */
public class CardMachine extends EventDispatcher
{
	public void dispatcher()
	{
		dispatcher(new EventPunchCard(EventPunchCard.type1,new Date()));
		dispatcher(new EventPunchCard(EventPunchCard.type2,new Date()));
	}
	
	public String toString()
	{
		return "我是打卡机器\n";
	}
	

	/**
	 * 测试打卡机器运转
	 * 
	 * @param args
	 */
	public static void main(String[] args)
	{
		CardMachine machine = new CardMachine();
		Listener screen = new Screen();
		Listener speaker = new Speaker();
		machine.addEventListener(EventPunchCard.type1, screen);
		machine.addEventListener(EventPunchCard.type1, speaker);
		machine.addEventListener(EventPunchCard.type2, speaker);
		machine.dispatcher();
		System.out.println("---------------------------------------------");
		machine.removeEventListener(EventPunchCard.type1, speaker);
		machine.dispatcher();
	}
}

 

/**
 * 油包
 * @author junlas
 *
 */
public class Event
{
	private String type;
	private EventDispatcher target;
	
	public Event(String type)
	{
		this.type = type;
	}
	
	public String getType()
	{
		return type;
	}

	public EventDispatcher getTarget()
	{
		return target;
	}

	public void setTarget(EventDispatcher target)
	{
		this.target = target;
	}
	
	
}

 

import java.util.LinkedList;
 import java.util.List;import java.util.Iterator;

/**
 * 第三方
 * @author junlas
 *
 */
public class EventDispatcher
{
	private List<Record<String, Listener>> foldSet = new LinkedList<Record<String,Listener>>();
	
	/**
	 * 发送
	 * @param evt
	 */
	public void dispatcher(Event evt)
	{
		evt.setTarget(this);
		Iterator<Record<String, Listener>> it = foldSet.iterator();
		while(it.hasNext())
		{
			Record<String, Listener> one = it.next();
			if(one.getKey().equals(evt.getType()))
			{
				one.getValue().performPunch((EventPunchCard)evt);
			}
		}
	}
	
	/**
	 * 注册监听
	 * @param type
	 * @param listener
	 */
	public void addEventListener(String type,Listener listener)
	{
		Record<String,Listener> one = new Record<String, Listener>(type,listener);
		foldSet.add(one);
	}
	
	/**
	 * 移除监听
	 * @param type
	 * @param listener
	 */
	public void removeEventListener(String type,Listener listener)
	{
		Iterator<Record<String, Listener>> it = foldSet.iterator();
		while(it.hasNext())
		{
			Record<String, Listener> one = it.next();
			if(one.getKey().equals(type) && one.getValue().equals(listener))
			{
				it.remove();
				break;
			}
			
		}
	}
	

}

 

//---------------------------定义打卡这个事件-----------------------EventPunchCard.java

import java.util.Date;

/**
 * 打卡事件<BR>
 * 在什么时间,由XX触发打卡这个操作(事件)
 * 
 * @author junlas
 * 
 */
public class EventPunchCard extends Event
{
	public static final String type1 = "screen";
	public static final String type2 = "speaker";
	
	private Date time; // 记录事件发生的时间

	public EventPunchCard(String type,Date time)
	{
		super(type);
		this.time = time;
	}

	public Date getTime()
	{
		return time;
	}

}

 

//----------------------------展现状态的接口-----------------------EventPunchCardListener.java

/**
 * 侦听者<br/>
 * 监听接口:监听打开的一系列状态的接口
 * 
 * @author junlas
 * 
 */
public interface Listener
{
	public void performPunch(EventPunchCard punch);
}

 

//----------------------------------展现的一个状态实现:显示--------------------------Screen.java

/**
 * 打卡机的显示屏<BR>
 * 用于显示打卡后的状态
 * 
 * @author junlas
 * 
 */
public class Screen implements Listener
{
	/**
	 * 显示余额
	 */
	@Override
	public void performPunch(EventPunchCard punch)
	{
		System.out.print("[Screen]:" + punch.getTime() + " ," + punch.getTarget());
		System.out.println("您的余额为XX !");
	}
}

 

//------------------------------展现的一个状态实现:声音------------------------------Speaker.java

/**
 * 打卡机的扬声器<BR>
 * 用于提醒打卡后的状态
 * 
 * @author junlas
 * 
 */
public class Speaker implements Listener
{
	/**
	 * 声音提醒
	 */
	public void performPunch(EventPunchCard punch)
	{
		System.out.print("[Speaker:]" + punch.getTime() + " ," + punch.getTarget());
		java.awt.Toolkit.getDefaultToolkit().beep();
	}
}

 

/**
 * 自定义的类型,至少可以看到数据类型,否则都是Object
 * @author junlas
 *
 * @param <K>
 * @param <V>
 */
public class Record<K,V>
{
	private Object[] record;
	
	public Record(K key,V value)
	{
		record = new Object[2];
		record[0] = key;
		record[1] = value;
	}
	
	public K getKey()
	{
		return (K)record[0];
	}
	
	public V getValue()
	{
		return (V)record[1];
	}
}

  个人感觉,这样子就使得发送方和接收方完全解耦了....

 欢迎大虾们多指点,多拍砖。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值