观察者模式可以使用两种方式实现,另外一种见https://blog.csdn.net/wgs_93/article/details/102587641
这里使用Java内置的方式来实现。该例子中使用的场景是公司与公司所属的各事业部之间的决定通信,公司发布公告通知,各事业部在接收到通知后分别做出的处理。
被观察者(公司):
package notice2;
import java.util.Observable;
/**
* 被观察者
*
* @author Scott
* @date 2019年10月17号
*/
public class AnotherHeadquartersNotice extends Observable {
/** 变更内容 */
private String notice;
public AnotherHeadquartersNotice() {
}
/**
* “被观察者” 发生变更时调用 notifyObservers() 方法通知观察者
* 发布新公告时触发通知公司下所有注册了的事业部
*
* @param notice 通知内容
*/
public void setNotice(String notice) {
this.notice = notice;
// 调用父类的改变方法,标识状态已经发生过变换了
setChanged();
// 调用父类的通知方法
notifyObservers();
}
public String getNotice() {
return notice;
}
}
公告处理接口:
package notice2;
/**
* 公告处理接口
* @author Scott
* @date 2019年10月17号
*/
public interface AnotherLowerHair {
/**
* 下发通知接口
*/
void lowerHair();
}
观察者A:
package notice2;
import java.util.Observable;
import java.util.Observer;
/**
* 观察者A
*
* @author Scott
* @date 2019年10月17号
*/
public class AnotherDivisionNine implements Observer, AnotherLowerHair {
private Observable observable;
private String notice;
/**
* 构造方法
* @param observable 被观察者
*/
public AnotherDivisionNine(Observable observable) {
this.observable = observable;
observable.addObserver(this);
}
/**
* 更新方法
* @param o 被观察者
* @param arg 数据对象
*/
@Override
public void update(Observable o, Object arg) {
if (o instanceof AnotherHeadquartersNotice) {
AnotherHeadquartersNotice anotherHeadquartersNotice = (AnotherHeadquartersNotice) o;
this.notice = anotherHeadquartersNotice.getNotice();
lowerHair();
}
}
/**
* 执行方法
*/
@Override
public void lowerHair() {
System.out.println("第九事业部公司决策通知:" + this.notice);
}
public Observable getObservable() {
return observable;
}
public String getNotice() {
return notice;
}
public void setNotice(String notice) {
this.notice = notice;
}
}
观察者B:
package notice2;
import java.util.Observable;
import java.util.Observer;
/**
* 观察者B
*
* @author Scott
* @date 2019年10月16号
*/
public class AnotherDivisionEight implements Observer, AnotherLowerHair {
private Observable observable;
private String notice;
/**
* 构造方法
*
* @param observable 被观察者
*/
public AnotherDivisionEight(Observable observable) {
this.observable = observable;
// 将自身注册为观察者
observable.addObserver(this);
}
/**
* 先判断 Observable 是否是 AnotherHeadquartersNotice
* 再通过 get 方法获取更新内容
* @param o 被观察者
* @param arg 数据对象
*/
@Override
public void update(Observable o, Object arg) {
if(o instanceof AnotherHeadquartersNotice) {
AnotherHeadquartersNotice anotherHeadquartersNotice = (AnotherHeadquartersNotice)o;
this.notice = anotherHeadquartersNotice.getNotice();
// 执行相关操作
lowerHair();
}
}
@Override
public void lowerHair() {
System.out.println("第八事业部公司决策通知:" + notice);
}
}
测试类:
package notice2;
public class Test {
public static void main(String[] args) {
AnotherHeadquartersNotice anotherHeadquartersNotice = new AnotherHeadquartersNotice();
AnotherDivisionNine anotherDivisionNine = new AnotherDivisionNine(anotherHeadquartersNotice);
AnotherDivisionEight anotherDivisionEight = new AnotherDivisionEight(anotherHeadquartersNotice);
anotherHeadquartersNotice.setNotice("根据公司发展需要,经研究决定,对第九事业部组织结构调整如下:XXXXXXXX ");
}
}
使用Java内置的方式来实现观察者模式,引入的包
java.util.Observable
java.util.Observer