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

 

输出:

 

目录
相关文章
|
API
Camera2预览方向、拍照方向设置
Camera2预览方向、拍照方向设置
1677 2
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16725 61
|
7天前
|
域名解析 人工智能 Linux
|
1天前
|
人工智能 安全 网络安全
|
15天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
云安全 数据采集 人工智能
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2257 7
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1185 2
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3754 22