zoukankan      html  css  js  c++  java
  • 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

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

    ● 扫码关注我们

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

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

     

  • 相关阅读:
    Windows下使用Nginx+tomcat配置负载均衡
    Windows系统搭建Mysql Cluster集群
    java.math.BigDecimal cannot be cast to java.lang.String解决方法
    Java从后台重定向(redirect)到另一个项目的方法
    Java代码中获取配置文件(config.properties)中内容的两种方法
    mybatis和redis整合 log4j打印sql语句
    Windows系统安装测试redis
    myeclipse 2014新建maven web 项目步骤
    myeclipse用maven搭建web项目后tomcat启动报找不到jar包解决办法
    《剑指offer》丑数
  • 原文地址:https://www.cnblogs.com/naimao/p/13446505.html
Copyright © 2011-2022 走看看