• 17、Observer 观察者模式


    htmljavaswing

    使Observer Pattern

    12

    123

    使

    • 使

    • ABBC使

     package com.mashibing.dp.observer.v9;
     
     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 Button {
     
     private List<ActionListener> actionListeners = new ArrayList<ActionListener>();
     
     public void buttonPressed() {
     ActionEvent e = new ActionEvent(System.currentTimeMillis(),this)
    for(int i=0; i<actionListeners.size(); i++) 
    ActionListener l = actionListeners.get(i)
    l.actionPerformed(e)

    }  

    public void addActionListener(ActionListener l) 
    actionListeners.add(l)

    }  

    interface ActionListener 
    public void actionPerformed(ActionEvent e)
    }  

    class MyActionListener implements ActionListener {  

    public void actionPerformed(ActionEvent e) 
    System.out.println("button pressed!")
    }  

    }  

    class MyActionListener2 implements ActionListener {  

    public void actionPerformed(ActionEvent e) 
    System.out.println("button pressed 2!")
    }  

    }  

    class ActionEvent {  

    long when
    Object source;  

    public ActionEvent(long when, Object source) 
    super()
    this.when = when
    this.source = source
    }   


    public long getWhen() 
    return when
    }  

    public Object getSource() 
    return source
    }  

    }

    button pressed!button pressed 2!

     package com.mashibing.dp.observer.v9;
     
     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(){  

    @Overrid
    public void windowClosing(WindowEvent e) 
    System.exit(0)
    }  

    })
    this.setLocation(400, 400)
    this.setVisible(true)
    }  

    public static void main(String[] args) 
    new TestFrame().launch()
    }  

    private class MyActionListener implements ActionListener { //Observer  

    @Overrid
            public void actionPerformed(ActionEvent e) 
    ((Button)e.getSource()).setLabel("press me again!")
    System.out.println("button pressed!")
    }  

    }  

    private class MyActionListener2 implements ActionListener {  

    @Overrid
            public void actionPerformed(ActionEvent e) 
    System.out.println("button pressed 2!")
    }  



    使

    使

    公众号发哥讲

    这是一个稍偏基础和偏技术的公众号,甚至其中包括一些可能阅读量很低的包含代码的技术文,不知道你是不是喜欢,期待你的关注。

    img

    如果你觉得文章还不错,就请点击右上角选择发送给朋友或者转发到朋友圈~

    ● 扫码关注我们

    据说看到好文章不推荐的人,服务器容易宕机!

    本文版权归发哥讲博客园共有,原创文章,未经允许不得转载,否则保留追究法律责任的权利。

     

  • 相关阅读:
    CET成绩批量查询
    c++常用库简介
    1985年以来微软、苹果、Google赚了多少钱?
    试试PT建站脚本
    补门~
    网站提交大全
    uniapp接入人身核验小程序
    【odoo】【知识杂谈】关于菜单及记录规则中“非”计算的改造
    【odoo】【知识杂谈】关于odoo二开模块规范的一点思考
    mysql拒绝访问
  • 原文地址:https://www.cnblogs.com/naimao/p/13446505.html
Copyright © 2020-2023  润新知