java设计模式——观察者模式

11 篇文章 0 订阅
4 篇文章 0 订阅

名词解释

Observer Pattern(又叫发布订阅模式)

定义了对象之间的一对多依赖,让多个观察者对象同时监听一个主体对象,当主体对象发生变化时,它的所有依赖者(观察者)都会收到通知并更新,属于行为型模式

写法

JDK自带

以订阅报纸为例,当人们订阅了报纸后,只要有新的报纸一发布,订阅了该报纸的人就能立即受到通知。

报纸 Newspaper 类:

public class Newspaper extends Observable {
    private String title;

    public String getTitle() {
        return title;
    }

    public void publishNew(String title){
        this.title = title;
        System.out.println("发布了标题为【"+ title +"】的新闻");
        this.setChanged();
        this.notifyObservers("新华社");
    }
}

订阅报纸的 People

public class People implements Observer {
    private String name ;

    public People(String name) {
        this.name = name;
    }

    @Override
    public void update(Observable o, Object arg) {
        Newspaper n = (Newspaper) o;
        System.out.println(this.name + "收到了新闻:"+n.getTitle()+",该新闻被【"+arg+"】发布");
    }
}

测试代码:

public class NewsPublishTest {
    public static void main(String[] args) {
        Newspaper news = new Newspaper();
        news.addObserver(new People("张三"));
        news.publishNew("今天是个好日子");
    }
}

输出结果:

发布了标题为【今天是个好日子】的新闻
张三收到了新闻:今天是个好日子,该新闻被【新华社】发布

Guava 实现

上面的JDK版本是将整个被观察的对象都继承了Observable类,同时还要让被观察者手动设置改变和发送通知

 this.setChanged();
 this.notifyObservers("新华社");

而且观察者那里也要先实现Observer接口,并实现update方法,这样显得代码很繁琐,换成Guava就很清爽了

报纸 Newspaper 类:完全没有代码侵入

public class Newspaper {
    private String title;

    public String getTitle() {
        return title;
    }

    public void publishNew(String title){
        this.title = title;
        System.out.println("发布了 guava 版本的新闻,标题为【"+title+"】");
    }
}

订阅报纸的 People 类,也很简单,只需要在处理订阅结果的方法上添加 Subscribe注解即可

public class People {

    private String name;

    public People(String name) {
        this.name = name;
    }

    @Subscribe
    public void receiveNews(Object o){
        Newspaper n = (Newspaper) o;
        System.out.println(this.name + "收到了新闻【"+n.getTitle()+"】");
    }
}

测试类:

public class TestGuavaNewspaper {
    public static void main(String[] args) {
      
        EventBus eventBus = new EventBus();  //消息总线
        Newspaper newspaper = new Newspaper();
        newspaper.publishNew("Guava不错");
        eventBus.register(new People("老李"));//先注册观察者
        eventBus.post(newspaper);//后发送被观察者
    }
}

手写版

实现的思路其实很简单,就是将观察者的实例和需要调用的方法放入到一个集合中(addListener方法干的事),然后在被观察者的相应动作(需要被观察的刚发)发生时,去集合中拿到观察者对象和相应方法,通过反射机制去调用观察者的观察方法,这样就能实现实时监听,代码如下:

首先,还是要先定义一个 Observable类,主要功能是可以添加观察者和手动触发观察者调用相关方法

public class Observable {
    private Map<String,Map<String,Object>> events = new HashMap<String, Map<String,Object>>();

    public void addListener(String eventType,Object object,String methodName) {//添加注册方法
        try {
            Map<String,Object> callbackInfo = new HashMap<String, Object>();
            callbackInfo.put("observer",object);
            callbackInfo.put("method",object.getClass().getMethod(methodName,Observable.class));
            events.put(eventType,callbackInfo);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
    public void trigger(String eventType){  //触发观察者的观察方法
        try {
            if(events.containsKey(eventType)){
                Map<String,Object> callbackInfo = events.get(eventType);
                Method m = (Method) callbackInfo.get("method");
                m.invoke(callbackInfo.get("observer"),this); //将本身作为参数传递给观察者
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我这里实现的很简单粗暴,还可以进一步优化代码,比如添加一个事件类,将相关的事件源、事件名称、回调方法进行封装,这里就继续扩展了。

Newspaper

public class Newspaper extends Observable {
    private String title;

    public String getTitle() {
        return title;
    }

    public void publishNew(String title){
        this.title = title;
        System.out.println("发布了标题为【"+ title +"】的新闻");
        this.trigger("publish");
    }
}

People类,没有代码侵入的痕迹

public class People {
    private String name ;

    public People(String name) {
        this.name = name;
    }

    public void receiveNew(Observable o) {
        Newspaper n = (Newspaper) o;
        System.out.println(this.name + "收到了新闻》》》"+n.getTitle());
    }
}

测试类:

public class NewsPublishTest {

    public static void main(String[] args) {
        Newspaper newspaper = new Newspaper();
        People laoLi = new People("老李");
        newspaper.addListener("publish",laoLi,"receiveNew");

        newspaper.publishNew("手写观察者模式上线了");
    }
}

输出结果:

发布了标题为【手写观察者模式上线了】的新闻
老李收到了新闻》》》手写观察者模式上线了

注意

这里的观察(监听)都是同步监听,在很多系统中,考虑到业务量太大,对时效性要求不是特别严格的情况下,用异步监听能提高系统的性能吞吐量,比如MQ,异步队列等

应用举例

  • Spring 中以Listener结尾的基本上都是观察者模式
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
观察者模式是一种常见的设计模式,它定义了一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖它的对象都会得到通知并自动更新。在这个模式中,被观察者对象通常称为主题(Subject),而观察者对象通常称为观察者(Observer)。 下面我们就以一个简单的天气预报系统为例来介绍观察者模式的使用。 首先,我们需要定义一个主题接口(Subject),它包含了添加、删除和通知观察者的方法: ```java public interface Subject { public void registerObserver(Observer o); public void removeObserver(Observer o); public void notifyObservers(); } ``` 然后,我们需要定义一个观察者接口(Observer),它包含了更新数据的方法: ```java public interface Observer { public void update(float temp, float humidity, float pressure); } ``` 接下来,我们需要定义一个具体的主题类(WeatherData),它实现了主题接口,并包含了一个列表来存储观察者对象,以及当前的温度、湿度和气压等数据: ```java import java.util.ArrayList; public class WeatherData implements Subject { private ArrayList<Observer> observers; private float temperature; private float humidity; private float pressure; public WeatherData() { observers = new ArrayList<Observer>(); } public void registerObserver(Observer o) { observers.add(o); } public void removeObserver(Observer o) { int i = observers.indexOf(o); if (i >= 0) { observers.remove(i); } } public void notifyObservers() { for (int i = 0; i < observers.size(); i++) { Observer observer = (Observer)observers.get(i); observer.update(temperature, humidity, pressure); } } public void measurementsChanged() { notifyObservers(); } public void setMeasurements(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; this.pressure = pressure; measurementsChanged(); } // other WeatherData methods here } ``` 最后,我们需要定义一个具体的观察者类(CurrentConditionsDisplay),它实现了观察者接口,并在更新数据时打印出当前的温度、湿度和气压等信息: ```java public class CurrentConditionsDisplay implements Observer { private float temperature; private float humidity; private Subject weatherData; public CurrentConditionsDisplay(Subject weatherData) { this.weatherData = weatherData; weatherData.registerObserver(this); } public void update(float temperature, float humidity, float pressure) { this.temperature = temperature; this.humidity = humidity; display(); } public void display() { System.out.println("Current conditions: " + temperature + "F degrees and " + humidity + "% humidity"); } } ``` 现在,我们可以创建一个天气预报系统,它包含了一个主题对象和一个观察者对象,并通过调用主题对象的方法来更新数据和通知观察者: ```java public class WeatherStation { public static void main(String[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); weatherData.setMeasurements(78, 90, 29.2f); } } ``` 以上就是一个简单的观察者模式的例子,它可以让我们更好地理解和应用这个常见的设计模式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值