观察者模式 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);
    }
}
复制代码

 

输出:

 

目录
相关文章
|
1月前
|
设计模式 JavaScript 开发者
详细讲解什么是观察者模式
详细讲解什么是观察者模式
|
6月前
|
设计模式 Java Spring
设计模式~观察者模式(Observer)-11
它属于行为型模式的一种。观察者模式定义了一种一对多的依赖关系,一个主题对象可被多个观察者对象同时监听。当这个主题对象状态变化时,会通知所有观察者对象并作出相应处理逻辑。 目录
45 0
|
10月前
|
设计模式 存储
设计模式-观察者模式(Observer)
设计模式-观察者模式(Observer)
72 0
|
11月前
|
设计模式 Java
Java设计模式-观察者模式(Observer)
Java设计模式-观察者模式(Observer)
|
开发者
观察者(observer)
观察者(observer)
104 0
观察者(observer)
|
设计模式 大数据
行为型-Observer
行为型设计模式主要解决的就是“类或对象之间的交互”问题。 原理及应用场景剖析 观察者模式(Observer Design Pattern)也被称为发布订阅模式(Publish-Subscribe Design Pattern)。在 GoF 的《设计模式》一书中,它的定义是这样的: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 翻译成中文就是:在对象之间
92 0
|
存储 设计模式 Java
浅谈JAVA设计模式之——观察者模式(Observer)
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
161 0
浅谈JAVA设计模式之——观察者模式(Observer)
观察者模式(Observer )
对象间的联动——观察者模式(一)对象间的联动——观察者模式(二)对象间的联动——观察者模式(三)对象间的联动——观察者模式(四)对象间的联动——观察者模式(五)对象间的联动——观察者模式(五)
735 0
|
监控 Java 设计模式