02观察者模式--模拟Awt(二)

1.public class AWTSimulation {


	public static void main(String[] args) {
		Buttton b = new Buttton();
		
		
		b.addActionListener(new MyActionListener());
		b.addActionListener(new MyActionListener2());
		
		b.buttonPressed();
		
	}

}

/**
 * 被观察者
 * @author Administrator
 *
 */
class Buttton{
	List<ActionListener> actionListeners = new ArrayList<ActionListener>();
	
	public void buttonPressed(){
		ActionEvent actionEvent = new ActionEvent(System.currentTimeMillis(),this);
		
		for(ActionListener al : actionListeners){
			al.actionPerformed(actionEvent);
		}
		
		
	}
	
	public void addActionListener(ActionListener actionListener){
		actionListeners.add(actionListener);
	}
	
	
}

/**
 * 观察者
 * @author Administrator
 *
 */
interface ActionListener{
	
	public void actionPerformed(ActionEvent actionEvent);
	
}

class MyActionListener implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		System.out.println("actionPerformed 1.....");
	}
}

class MyActionListener2 implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		System.out.println("actionPerformed 2.....");
	}
}

class ActionEvent{
	//事件发生时间
	long when;
	
	//事件源对象
	Object source;

	public ActionEvent(long when,Object source) {
		this.source = source;
		this.when = when;
	}
	
	public long getWhen(){
		return when;
	}
	
	public Object getSource(){
		return source;
	}
	
	
	
	
	
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值