本文主要是介绍2 观察者模式(设计模式笔记),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
2 观察者模式(别名:发布-订阅)
概念
- 定义对象间的一种一对多的依赖关系,当一个对象状态发生变化时,所以依赖于它的对象都得到通知并被自动更新。
模式的结构与使用
角色
- 主题(Subject)
- 观察者(Observer)
- 具体主题(ConcreteSubject)
- 具体观察者(ConcreteObserver)
结构
Subject依赖于Observer最重要!!!
package Observer;/*** @author chx* @version 1.0* @description: TODO* @date 2021/3/15 0015 8:52*/
public interface Subject {void addOberver(Oberserve o);void deleteOberver(Oberserve o);//通知void notifyOberver();
}
package Observer;/*** @author chx* @version 1.0* @description: TODO* @date 2021/3/15 0015 8:53*/
public interface Oberserve {void hearTelephone(String hearMsg);
}
package Observer;import java.util.ArrayList;
import java.util.List;/*** @author chx* @version 1.0* @description: TODO* @date 2021/3/15 0015 8:56*/
public class SeekJobCenter implements Subject{//通知消息private String msg;//判断消息是否变化private boolean changed;//观察者集合private List<Oberserve> oberserves;public SeekJobCenter() {this.msg = "";this.changed = false;this.oberserves = new ArrayList<Oberserve>();}@Overridepublic void addOberver(Oberserve o) {if(!oberserves.contains(o)){oberserves.add(o);}}@Overridepublic void deleteOberver(Oberserve o) {if(oberserves.contains(o)){oberserves.remove(o);}}@Overridepublic void notifyOberver() {if(changed){for (Oberserve oberserve : oberserves) {oberserve.hearTelephone(msg);}}}public void giveNewMsg(String msg){if(msg.equals(this.msg)){changed=false;}else {System.out.println("发布了新消息:"+msg);changed = true;this.msg = msg;notifyOberver();}}
}
package Observer;/*** @author chx* @version 1.0* @description: TODO* @date 2021/3/15 0015 9:07*/
public class UniverStudent implements Oberserve{private Subject subject;public UniverStudent(Subject subject) {this.subject = subject;//自动添加观察者(关联的主要作用)this.subject.addOberver(this);System.out.println("我是物电专业的");}@Overridepublic void hearTelephone(String hearMsg) {if (hearMsg.contains("嵌入式") || hearMsg.contains("物联网")){System.out.println("感兴趣 "+hearMsg);}else {System.out.println("不感兴趣");}}
}
package Observer;/*** @author chx* @version 1.0* @description: TODO* @date 2021/3/15 0015 9:07*/
public class Returness implements Oberserve{private Subject subject;public Returness(Subject subject) {this.subject = subject;//自动添加观察者(关联的主要作用)this.subject.addOberver(this);System.out.println("我是计算机专业的");}@Overridepublic void hearTelephone(String hearMsg) {if (hearMsg.contains("Java") || hearMsg.contains("Python")){System.out.println("感兴趣 "+hearMsg);}else {System.out.println("不感兴趣");}}
}
package Observer;/*** @author chx* @version 1.0* @description: TODO* @date 2021/3/15 0015 9:16*/
public class Application {public static void main(String[] args) {SeekJobCenter center = new SeekJobCenter();UniverStudent us = new UniverStudent(center);Returness rt = new Returness(center);center.giveNewMsg("需要9个嵌入式开发");}
}
优点
- 具体主题和具体观察者是松耦合关系。由于主题(Subject)接口仅仅依赖于观察者(Observer)接口,因此具体主题只是知道它的观察者是实现观察者(Observer)接口的某个类的实例,但不需要知道具体是哪个类。同样,由于观察者仅仅关联于主题(Subject)接口,因此具体观察者只是知道它依赖的主题是实现主题(subject)接口的某个类的实例,但不需要知道具体是哪个类。(即两者都只需和接口打交道,相互扩展性就会高了很多)
- 观察模式满足“开-闭原则”。主题(Subject)接口仅仅依赖于观察者(Observer)接口,这样,我们就可以让创建具体主题的类也仅仅是依赖于观察者(Observer)接口,因此如果增加新的实现观察者(Observer)接口的类,不必修改创建具体主题的类的代码。同样,创建具体观察者的类仅仅依赖于主题(Observer)接口,如果增加新的实现主题(Subject)接口的类,也不必修改创建具体观察者类的代码。
这篇关于2 观察者模式(设计模式笔记)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!