简述
当对象间存在一对多关系时,则使用观察者模式(Observer Pattern)。比如,当一个对象被修改时,则会自动通知依赖它的对象。观察者模式属于行为型模式。
- 自己实现观察者模式
观察者的接口
package com.example.demodesign00;
/**
* @author lzb
* @date 2022/7/30 11:16
*
*/
public interface Observer {
/**
*
*/
void update(String msg);
}
被观察者的接口
package com.example.demodesign00;
/**
* @author lzb
* @date 2022/7/30 10:31
*/
public interface Subject {
/**
* 注册一个观察者
* @param observer
*/
void registerObserver(Observer observer);
/**
* 移除一个观察者
* @param observer
*/
void removeObserver(Observer observer);
/**
* 通知所有的观察者
*/
void notifyObserver();
}
具体的被观察者
package com.example.demodesign00;
import java.util.ArrayList;
import java.util.List;
/**
* @author lzb
* @date 2022/7/30 11:17
*/
public class CreateSubject implements Subject{
/**
* 观察者列表
*/
private List<Observer> observerList = new ArrayList<>();
public void setMsg(String msg) {
this.msg = msg;
notifyObserver();
}
private String msg;
@Override
public void registerObserver(Observer observer) {
observerList.add(observer);
}
@Override
public void removeObserver(Observer observer) {
int i = observerList.indexOf(observer);
if(i>=0){
observerList.remove(i);
}
}
@Override
public void notifyObserver() {
for (Observer observer:observerList) {
observer.update(msg);
}
}
}
观察者1号。对被观察者进行关注
package com.example.demodesign00;
/**
* @author lzb
* @date 2022/7/30 11:17
*/
public class CreateObserver implements Observer{
private Subject subject;
public CreateObserver(Subject subject){
this.subject = subject;
subject.registerObserver(this);
}
@Override
public void update(String msg) {
System.out.println("获得号码"+msg+"记下来");
}
}
观察者二号,也观察了CreateSubject
package com.example.demodesign00;
/**
* @author lzb
* @date 2022/7/30 12:27
*/
public class CreateObserver1 implements Observer{
private Subject subject;
//构造方法中注册
public CreateObserver1(Subject subject){
this.subject = subject;
//将自己这个观察者 注册 主题中。
subject.registerObserver(this);
}
@Override
public void update(String msg) {
System.out.println("我是第二个观察者,收到的消息是:"+msg);
}
}
package com.example.demodesign00;
/**
* @author lzb
* @date 2022/7/30 12:35
*/
public class Test {
public static void main(String[] args) {
CreateSubject createSubject = new CreateSubject();
CreateObserver createObserver = new CreateObserver(createSubject);
CreateObserver1 createObserver1 = new CreateObserver1(createSubject);
createSubject.setMsg("好好学习,天天向上");
}
}
具体运行结果:
以上就是自己实现的观察者模式,但是,当然了java中已经帮我们实现了观察者模式,借助于java.util.Observable和java.util.Observer。
- 用java自带的观察者设计模式实现
观察者1号,这里观察者,不只观察3d,还观察了双色球类,实现了多观察
package com.example.demodesign01;
import java.util.Observable;
import java.util.Observer;
/**
* @author lzb
* @date 2022/7/30 12:59
* 观察者
*/
public class Observer1 implements Observer {
public void registerSubject(Observable observable){
observable.addObserver(this);
}
@Override
public void update(Observable o, Object arg) {
if( o instanceof SubjectFor3d){
SubjectFor3d subjectFor3d = (SubjectFor3d) o;
System.out.println("3d"+subjectFor3d.getMsg());
}
if( o instanceof SubjectForSSQ ){
SubjectForSSQ subjectForSSQ = (SubjectForSSQ) o;
System.out.println("ssq"+subjectForSSQ.getMsg());
}
}
}
被观察者,3d类
package com.example.demodesign01;
import java.util.Observable;
/**
* @author lzb
* @date 2022/7/30 12:53
* 3d 主题类
*/
public class SubjectFor3d extends Observable {
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
//值的变更
setChanged();
//变更后通知
notifyObservers();
}
private String msg;
}
被观察者2号,双色球类
package com.example.demodesign01;
import java.util.Observable;
/**
* @author lzb
* @date 2022/7/30 12:57
*/
public class SubjectForSSQ extends Observable {
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
//设置值
setChanged();
//设置值之后的通知
notifyObservers();
}
private String msg;
}
运行结果
package com.example.demodesign01;
/**
* @author lzb
* @date 2022/7/30 12:51
*/
public class Test {
public static void main(String[] args) {
SubjectForSSQ subjectForSSQ = new SubjectForSSQ();
Observer1 observer1 = new Observer1();
observer1.registerSubject(subjectForSSQ);
subjectForSSQ.setMsg("双色球开奖号码:1233423");
SubjectFor3d subjectFor3d = new SubjectFor3d();
observer1.registerSubject(subjectFor3d);
subjectFor3d.setMsg("3d开奖号码:123123");
}
}
输出结果
ssq双色球开奖号码:1233423
3d3d开奖号码:123123
- SpingBoot 实现观察者模式
后面补充