马士兵设计模式2之观察者模式模拟AWT

package _09_awt;

 

 

import java.util.ArrayList;

import java.util.List;

 

public class Test {

 

/**

 * @param args

 * 模仿  AWT

 */

public static void main(String[] args) {

 

Button b=new Button();

b.addActionListener(new MyActionListener());

b.buttonPressed();//模拟按下按钮

}

 

}

 

 

class Button{

 

private List<ActionListener> actionListeners=new ArrayList<ActionListener>();

public void buttonPressed() {

ActionEvent e=new ActionEvent(System.currentTimeMillis(),this);

for(ActionListener actionListener:actionListeners){

actionListener.actionPerformed(e);

}

}

public void addActionListener(ActionListener l){

actionListeners.add(l);

}

}

 

interface ActionListener{

public void actionPerformed(ActionEvent e);

}

 

 

class ActionEvent{

long when;

Object source;

public ActionEvent(long when,Object source){

this.when=when;

this.source=source;

}

public long getWhen(){

return when;

}

public Object getSource(){

return source;

}

}

 

class MyActionListener implements ActionListener{

 

@Override

public void actionPerformed(ActionEvent e) {

System.out.println("button pressed");

System.out.println("time:"+e.getWhen());

System.out.println("source:"+e.getSource());

}

 

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值