zoukankan      html  css  js  c++  java
  • 117.Java观察者设计模式

    设计模式

    设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。

     观察者模式

       有时又被称为

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

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

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

          或从属者<Dependents>模式)

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

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

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

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

       此种模式通常被用来实作事件处理系统。

       有多个观察者时,不可以依赖特定的通知次序。

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

    气象站:

    public class WeatherStation {
        
        private String weather;
        
        String[] weathers = {"下雨","下雪","下冰雹","出太阳"};
        
        static    List<BookWeather> list = new ArrayList<BookWeather>();
        
        Random random = new Random();
        
        
        public void startWork(){
            
            new Thread(){
                
                @Override
                public void run() {
                    while(true){
                        updateWeather();
                        try {
                            Thread.sleep(random.nextInt(1000)+500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
        public void updateWeather(){
            weather = weathers[random.nextInt(4)];
            System.out.println("天气:"+ weather);
        }        
        
        
        public String getWeather() {
            return weather;
        }

    人:

    public class Person implements BookWeather {
        
        String name;
        
        public  Person(String name){
            this.name = name;
        }
        
        private WeatherStation station ;
        
        public Person(String name,WeatherStation station){
            this(name);
            this.station = station;
        }
        
        //下雨","下雪 ","下冰雹","出太阳"
        @Override
        public void notifyWeather() {
            String weather = station.getWeather();
            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("小明",station);
            while(true){
                p1.notifyWeather();
                Thread.sleep(2000);
            }
        }

    问题:天气变化两三次,小明才知道一次。

    解决方案 :

    package cn.itcast.test;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Random;
    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();
                        try {
                            Thread.sleep(random.nextInt(1000)+500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }.start();
        }
        public void updateWeather(){
            weather = weathers[random.nextInt(4)];
            System.out.println("天气:"+ weather);
            for(BookWeather item : list){
                item.notifyWeather(weather);
            }
        }        
        
        
        public String getWeather() {
            return weather;
        }

    人:

    public class Person implements BookWeather {
        
        String name;
        
        public  Person(String name){
            this.name = name;
        }
        
        private WeatherStation station ;
        
        public Person(String name,WeatherStation station){
            this(name);
            this.station = station;
        }
        
        //下雨","下雪 ","下冰雹","出太阳"
        @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 interface BookWeather {
        
        public void notifyWeather(String weather);
    }
    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("小红");
            Person p3 = new Person("小青 ");
            station.addListaner(p1);
            station.addListaner(p2);
            station.addListaner(p3);
            
        }
    }
    author@nohert
  • 相关阅读:
    Java 异常基础详解
    Try-with-resources
    Java集合详解
    Java面向对象之多态
    Java面向对象之继承
    Java面向对象之封装
    Java 接口
    Java抽象类
    Java类和对象
    Java 数组结构
  • 原文地址:https://www.cnblogs.com/gzgBlog/p/13670767.html
Copyright © 2011-2022 走看看