它定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象。这个主题对象在状态变化时,会通知所有的观察者对象,使他们能够自动更新自己
我们每个人都在使用微信,微信中有两种角色,一种是微信公众号,一种是普通用户;普通用户可以关注微信公众号;如果微信公众号发布了新文章,那么关注他的用户将会收到通知;这就是最常见的观察者模式
Subject
- package com.designer.observe.Subject;
-
- import com.designer.observe.Observer.Observer;
-
- /**
- * 抽象主题(Subject)角色
- * 用来模拟微信公众号抽象模板
- */
- public interface Subject {
-
- void registerObserver(Observer o);
-
- //移除监听器
- void removeObserver(Observer o);
-
- //发送消息
- void notifyObservers();
- }
ConcreteSubject
- package com.designer.observe.ConcreteSubject;
-
- import com.designer.observe.News;
- import com.designer.observe.Observer.Observer;
- import com.designer.observe.Subject.Subject;
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * 具体主题(Concrete Subject)角色
- * 用来模拟微信公众号
- */
- public class ConcreteSubject implements Subject {
-
- private List<Observer> observers;
- private News news;
-
- public ConcreteSubject(){
- this.observers = new ArrayList<>();
- }
-
- public void setNews(News news) {
- this.news = news;
- }
-
- @Override
- public void registerObserver(Observer o) {
- this.observers.add(o);
- }
-
- @Override
- public void removeObserver(Observer o) {
- this.observers.remove(o);
- }
-
- @Override
- public void notifyObservers() {
- for (Observer observer: this.observers) {
- observer.update(this.news);
- }
- }
- }
Observer
- package com.designer.observe.Observer;
-
- import com.designer.observe.News;
-
- /**
- * 抽象观察者(Observer)角色
- * 用来模拟 关注微信公众号的粉丝 模板
- */
- public interface Observer {
-
- void update(News news);
- }
ConcreteObserver
- package com.designer.observe.ConcreteObserver;
-
- import com.designer.observe.News;
- import com.designer.observe.Observer.Observer;
-
- /**
- * 具体主题(Concrete Subject)角色
- * 用来模拟 微信公众号的具体粉丝
- */
- public class ConcreteObserver implements Observer {
-
- private String name;
- private News news;
-
- public ConcreteObserver(String name){
- this.name = name;
- }
-
- @Override
- public void update(News news) {
- this.news = news;
- showNews();
- }
-
- private void showNews(){
- System.out.println("尊敬的" + this.name + ",您订阅的 " + this.news.getAuthor() + " 刚刚发布了" + this.news.getTitle() + " 内容如下:");
- System.out.println(this.news.getContext());
- System.out.println();
- }
- }
News
- package com.designer.observe;
-
- /**
- * 新闻类
- */
- public class News {
-
- private String author;
- private String title;
- private String context;
-
- public News(String author, String title, String context) {
- this.author = author;
- this.title = title;
- this.context = context;
- }
-
- public String getAuthor() {
- return this.author;
- }
-
- public String getTitle() {
- return this.title;
- }
-
- public String getContext() {
- return this.context;
- }
- }
Test
- package com.designer.observe;
-
- import com.designer.observe.ConcreteObserver.ConcreteObserver;
- import com.designer.observe.ConcreteSubject.ConcreteSubject;
-
- public class Test {
-
- public static void main(String[] args) {
-
- // 微信公众号
- ConcreteSubject subject = new ConcreteSubject();
-
- // 粉丝
- ConcreteObserver observer1 = new ConcreteObserver("粉丝1");
- ConcreteObserver observer2 = new ConcreteObserver("粉丝2");
- ConcreteObserver observer3 = new ConcreteObserver("粉丝3");
-
- // 粉丝关注微信公众号
- subject.registerObserver(observer1);
- subject.registerObserver(observer2);
- subject.registerObserver(observer3);
- // 粉丝取消关注微信公众号
- subject.removeObserver(observer2);
-
- // 微信公众号发布文章
- News news = new News("IT蓝月", "美国宣布对华取消关税!", "2022年x月x日,美国最终因为通货膨胀的压力,宣布对华取消了关税。");
- subject.setNews(news);
-
- // 推送给粉丝
- subject.notifyObservers();
- }
- }