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