zoukankan      html  css  js  c++  java
  • 观察者模式 Observer

    观察者模式 Observer

      这是软件设计模式的一种。

      又被称为:

      发布-订阅<Publish/Subscribe>模式、

      模型-视图<Model/View>模式、

      源-收听者<Source/Listener>模式

      或从属者<Dependents>模式)

        观察者模式(Observer)完美的将观察者和被观察的对象分离开。

      此种模式中,一个目标物件管理所有相依于它的观察者物件,并且在它本身的状态改变时主动发出通知。

      这通常透过呼叫各观察者所提供的方法来实现。

      观察者设计模式:观察者设计模式解决的问题时当一个对象发生指定的动作时,要通过另外一个对象做出相应的处理。

      观察者设计模式的步骤:
          1. 当前目前对象发生指定的动作是,要通知另外一个对象做出相应的处理,这时候应该把对方的相应处理方法定义在接口上。
          2. 在当前对象维护接口的引用,当当前对象发生指定的动作这时候即可调用接口中的方法了。 

      此种模式通常被用来实作事件处理系统。   有多个观察者时,不可以依赖特定的通知次序。

      Swing大量使用观察者模式,许多GUI框架也是如此。

    案例说明:  编写一个气象站 类、一个工人 类  、一个学生类,当气象站更新天气 的时候,要通知 人和学生 做出相应的处理。

    public class WeatherStation {
        private String weather;//当前天气
        String[] weathers = {"下雨","下雪","下冰雹","出太阳"};
        private static List<BookWeather> list = new ArrayList<BookWeather>();//该集合中存储的都是需要收听天气预报的人
        Random random = new Random();
        public void addListaner(BookWeather e){
            list.add(e);
        }
        public void startWork(){ //开始工作
            new Thread(){
                @Override
                public void run() {
                    while(true){
                        updateWeather();
                for(BookWeather item : list)
                { item.notifyWeather(weather); //更新天气给收听者 }
    try { Thread.sleep(random.nextInt(1000)+500);//每0.5-1.5秒更新一次天气 } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } public void updateWeather(){//更新天气 weather = weathers[random.nextInt(4)]; System.out.println("天气:"+ weather); }
    }
    public interface BookWeather { //订阅天气预报的接口
        public void notifyWeather(String weather);
    }
    public class Person implements BookWeather {//Person要根据天气做出相应的处理
        String name;
        public  Person(String name){
            this.name = name;
        }
        //下雨","下雪 ","下冰雹","出太阳"
        @Override
        public void notifyWeather(String weather) {
            if("下雨".equals(weather)){
                System.out.println(name+"打着雨伞 上班");
            }else if("下雪".equals(weather)){
                System.out.println(name+"戴着被子 上班");
            }else if("下冰雹".equals(weather)){
                System.out.println(name+"带着头盔 上班");
            }else if("出太阳".equals(weather)){
                System.out.println(name+"嗮着太阳 上班");
            }
        }
    }
    public class Student implements BookWeather {
        String name;
        public Student(String name) {
            this.name = name;
        }
        @Override
        public void notifyWeather(String weather) {
            if ("下雨".equals(weather)) {
                System.out.println(name + "打着雨伞 上学");
            } else if ("下雪".equals(weather)) {
                System.out.println(name + "戴着被子 上学");
            } else if ("下冰雹".equals(weather)) {
                System.out.println(name + "带着头盔 上学");
            } else if ("出太阳".equals(weather)) {
                System.out.println(name + "嗮着太阳 上学");
            }
        }
    }
    public class test {
        public static void main(String[] args) throws InterruptedException {
            WeatherStation station = new WeatherStation();
            station.startWork();
            Person p1 = new Person("小明");
            Person p2 = new Person("小红");
            Student p3 = new Student("小青 ");
            station.addListaner(p1);
            station.addListaner(p2);
            station.addListaner(p3);
        }
    }

    输出:

  • 相关阅读:
    Unix命令大全
    vs2008 与 IE8出现的兼容性问题
    Java 创建文件、文件夹以及临时文件
    如何修改Wamp中mysql默认空密码
    PAT 乙级真题 1003.数素数
    Tags support in htmlText flash as3
    DelphiXE4 FireMonkey 试玩记录,开发IOS应用 还是移植
    10 Great iphone App Review sites to Promote your Apps!
    HTML tags in textfield
    Delphi XE4 IOS 开发, "No eligible applications were found“
  • 原文地址:https://www.cnblogs.com/loveincode/p/5342800.html
Copyright © 2011-2022 走看看