zoukankan      html  css  js  c++  java
  • 1 观察者模式 observer 模拟监听器的实现

    参考(http://blog.csdn.net/allwefantasy/article/details/3062277)

    假设我有一台电脑。电脑里面有一个事件,当你按下电源按钮的时候,那么显示器会亮,电源指示灯也会亮。
    这是典型的一个类似于GUI中按钮事件模拟。
    我们来分析一下需要几个类
    电 脑肯定是需要的,我们得出Computer类。Computer类内部有一个按电源按钮的动作,我们叫做clickButton.这个事件我们叫做 EventPowerOn.于是得出了EventPowerOn类。另外,对比GUI里面的button,他们都会添加监听器,所以应该有个监听器类,我 们叫做EventPowerOnListener。好到这里我们基本上找出了几个必须的类Computer,EventPowerOn ,EventPowerOnListener

    compute类

    package com.da.tool.mode;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Compute {
        private List<EventPowerOnListener> listeners=new ArrayList();
    
        public void addEventPowerOnListener(EventPowerOnListener eventPowerOnListener) {
            listeners.add(eventPowerOnListener);
        }
    
        public void click(){
            for(EventPowerOnListener temp:listeners) {
                temp.performanceToPowerOn(new EventPowerOn(System.currentTimeMillis(),this));
            }
        }
    }

    电源键被按下时间类 EventPowerOn

    package com.da.tool.mode;
    
    public class EventPowerOn {
    
        private Object source;//事件的来源,也就是时间是由哪一个对象激发的
        private long time;//这个属性是随意添加的 比如社么时候电源按钮被按下
    
        public EventPowerOn( long time, Object object) {
            this.source = object;
            this.time = time;
        }
        public Object getObject() {
            return source;
        }
        public void setObject(Object object) {
            this.source = object;
        }
        public long getTime() {
            return time;
        }
        public void setTime(long time) {
            this.time = time;
        }
    }

    监听接口类

    package com.da.tool.mode;
    
    public interface EventPowerOnListener {
        public void performanceToPowerOn(EventPowerOn eventPowerOn);//改方法是电源按钮被按后应的响应
    }

    灯光类

    package com.da.tool.mode;
    
    public class Light implements EventPowerOnListener{
        public void performanceToPowerOn(EventPowerOn eventPowerOn) {
            System.out.println(eventPowerOn.getTime());
            System.out.println("power light is on");
        }
    }

    屏幕类

    package com.da.tool.mode;
    
    public class Screen  implements EventPowerOnListener {
    
        public void performanceToPowerOn(EventPowerOn eventPowerOn) {
            System.out.println(eventPowerOn.getTime());
            System.out.println("Screen is on");
        }
    }

    测试类

    package com.da.tool.mode;
    
    public class testObserver {
        public static void main(String[] args){
            Compute computer=new Compute();
            Screen screen=new Screen();
            Light light=new Light();
            computer.addEventPowerOnListener(screen);
            computer.addEventPowerOnListener(light);
            computer.click();
        };
    }

    测试结果:

    1491806527242
    Screen is on
    1491806527242
    power light is on

  • 相关阅读:
    HDU 2899 Strange fuction
    HDU 2899 Strange fuction
    HDU 2199 Can you solve this equation?
    HDU 2199 Can you solve this equation?
    Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
    Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
    Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
    Java实现 LeetCode 699 掉落的方块(线段树?)
    Java实现 LeetCode 699 掉落的方块(线段树?)
    Java实现 LeetCode 699 掉落的方块(线段树?)
  • 原文地址:https://www.cnblogs.com/yangh2016/p/6689105.html
Copyright © 2011-2022 走看看