• 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
  • 相关阅读:
    自学Linux Shell2.1-进入shell命令行
    自学Linux Shell1.3-Linux文件系统
    自学Linux Shell1.2-Linux目录结构
    自学Linux Shell1.1-Linux初识
    03 自学Aruba之2.4GHz及5GHz无线信道
    02 自学Aruba之无线频段---ISM频段及UNII频段
    01 自学Aruba之功率单位和相对单位
    1.Zabbix报错信息:It probably means that the systems requires more physical memory.
    自学Aruba5.3.4-Aruba安全认证-有PEFNG 许可证环境的认证配置802.1x
    自学Aruba5.3.3-Aruba安全认证-有PEFNG 许可证环境的认证配置Captive-Portal
  • 原文地址:https://www.cnblogs.com/tenWood/p/9211065.html
Copyright © 2020-2023  润新知