设计模式-->行为型模式-->观察者模式

本文介绍观察者模式在气象应用中的实践,通过WeatherDate类作为Subject,多个Observer如CurrentConditons和Baidu实现订阅与通知,展示了如何维护用户订阅并确保代码的可扩展性。
摘要由CSDN通过智能技术生成

观察者(observer)模式原理

observer:接收输入

观察者模式:对象之间多对一依赖的一种设计方案,被依赖的对象为Subject,依赖的对象为Observer,Subject通知Observer变化 比如一个定奶项目:这里的奶站是Subject 是1这一方,用户Observer是多的一方

一个应用在天气气象通知的观察者demo

 

//一个观察者 
package com.ljb.meteorologicalservicesPro;

/**
 * Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
 *
 * @author Sw_Ljb
 * @version 1.0
 * @ClassName Observe
 * @Date 2022/4/21下午4:43
 */
public interface Observe {
    void update(int temperature, int humidity, float Airpressure);
}

//一个订阅者抽象类
package com.ljb.meteorologicalservicesPro;

/**
 * Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
 *
 * @author Sw_Ljb
 * @version 1.0
 * @ClassName Subject
 * @Date 2022/4/21下午4:45
 */
public interface Subject {
    void regisiterObject(Observe o);
    void removeObject(Observe o);
    void notifyObject();
}
//一个气象站
package com.ljb.meteorologicalservicesPro;

import com.ljb.meteorologicalservices.CurrentConditons;

import java.util.ArrayList;

/**
 * Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
 *
 * @author Sw_Ljb
 * @version 1.0
 * @ClassName WeatherDate
 * @Date 2022/4/21下午4:18
 */
public class WeatherDate implements Subject{

    private int temperature;
    private int humidity;
    private float Airpressure;

    //集合
    private ArrayList<Observe> arrayList;
    //private CurrentConditons currentConditons;

    //导入第三方
    public WeatherDate() {
        arrayList = new ArrayList<Observe>();
    }

    public int getTemperature() {
        return temperature;
    }

    public int getHumidity() {
        return humidity;
    }

    public float getAirpressure() {
        return Airpressure;
    }


    public void dateChange(){

//        currentConditons.update(getTemperature(),getHumidity(),getAirpressure());
        notifyObject();
    }

    public void setData(int temperature,int humidity,float Airpressure){
        this.temperature = temperature;
        this.humidity= humidity;
        this.Airpressure = Airpressure;
        dateChange();
    }

    @Override
    public void regisiterObject(Observe o) {
            arrayList.add(o);
    }

    @Override
    public void removeObject(Observe o) {
        //移除前先判断有没有
        if (arrayList.contains(o))
        arrayList.remove(o);
    }

    @Override
    public void notifyObject() {
        for (Observe o:arrayList
             ) {
            o.update(getTemperature(),getHumidity(),getAirpressure());
        }
    }
}
//一个第三方
package com.ljb.meteorologicalservicesPro;

/**
 * Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
 *
 * @author Sw_Ljb
 * @version 1.0
 * @ClassName CurrentConditons
 * @Date 2022/4/21下午4:28
 */
public class CurrentConditons implements Observe{
    private int temperature;
    private int humidity;
    private float Airpressure;


    public void update(int temperature, int humidity, float Airpressure) {
        this.Airpressure=Airpressure;
        this.humidity=humidity;
        this.temperature=temperature;
        display();
    }

    public void display(){
        System.out.println("today mTem:"+temperature);
        System.out.println("today mHum:"+humidity);
        System.out.println("today mAir:"+Airpressure);
    }
}
//第二个第三方
package com.ljb.meteorologicalservicesPro;

/**
 * Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
 *
 * @author Sw_Ljb
 * @version 1.0
 * @ClassName Baidu
 * @Date 2022/4/21下午5:14
 */
public class Baidu implements Observe{
    private int temperature;
    private int humidity;
    private float Airpressure;


    public void update(int temperature, int humidity, float Airpressure) {
        this.Airpressure=Airpressure;
        this.humidity=humidity;
        this.temperature=temperature;
        display();
    }

    public void display(){
        System.out.println("===百度===");
        System.out.println("baidu today mTem:"+temperature);
        System.out.println("baidu today mHum:"+humidity);
        System.out.println("baidu today mAir:"+Airpressure);
    }
}

 

//客户端
package com.ljb.meteorologicalservicesPro;

/**
 * Copyright (C), 2022-2030, Chongqing Mac info. Co., Ltd.
 *
 * @author Sw_Ljb
 * @version 1.0
 * @ClassName Client
 * @Date 2022/4/21下午5:11
 */
public class Client {
    public static void main(String[] args) {
        WeatherDate weatherDate = new WeatherDate();
        CurrentConditons currentConditons = new CurrentConditons();
        Baidu baidu = new Baidu();
        weatherDate.regisiterObject(currentConditons);
        System.out.println("============");
        weatherDate.regisiterObject(baidu);
        weatherDate.setData(23,43,43.4f);


    }
}

好处:

1、观察者模式设计后 会以集合的方式来管理用户(Observer) 包括注册 移动 通知

2、这样我们增加观察者(理解为一个新的公告板) 就不需要去修改核心类weatherData不会修改代码 遵循了OCP原则

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值