java设计模式-Observer(2)

java设计模式-Observer(2)

一、模拟AWT事件处理          
回顾一下JDK里面按下一个Button,有件事发生,这个东西怎么写:
package com.cy.dp.observer.awt;

import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestFrame extends Frame{
    
    public void launch(){
        Button b = new Button("press me");
        b.addActionListener(new MyActionListener());
        b.addActionListener(new MyActionListener2());
        this.add(b);
        this.pack();

        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        this.setVisible(true);
    }
    
    public static void main(String[] args) {
        new TestFrame().launch();
    }
    
    private class MyActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("button pressed!");
        }
    }
    
    private class MyActionListener2 implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("button pressed 2!");
        }
    }
}

二、自己手写模拟AWT事件处理        

代码:

package com.cy.dp.observer.awt;

import java.util.ArrayList;
import java.util.List;

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

class MyActionListener implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("button pressed, time:"+e.getWhen()+", source:"+e.getSource());
    }
}
class MyActionListener2 implements ActionListener{
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("button pressed 2, time:"+e.getWhen()+", source:"+e.getSource());
    }
}



/********************************对于使用java.awt包,下面代码就不可见了****************************************/
/**
 * 自己模拟的Button
 */
class Button{
    private List<ActionListener> listeners = new ArrayList<ActionListener>();
    
    public void addActionListener(ActionListener l){
        this.listeners.add(l);
    }
    
    //模拟button被按下
    public void buttonPressed() {
        ActionEvent e = new ActionEvent(this, System.currentTimeMillis());
        for(ActionListener listener : listeners){
            listener.actionPerformed(e);
        }
    }
    
}

interface ActionListener{
    public void actionPerformed(ActionEvent e);
}

/**
 * 自己模拟的事件
 */
class ActionEvent{
    private long when;
    private Object source;
    
    public ActionEvent(Object source, long when){
        this.source = source;
        this.when = when;
    }
    
    public long getWhen(){
        return when;
    }
    public Object getSource(){
        return source;
    }
}

console:

button pressed, time:1529588100490, source:com.cy.dp.observer.awt.Button@2a139a55
button pressed 2, time:1529588100490, source:com.cy.dp.observer.awt.Button@2a139a55