模式的组成结构
抽象主题(Subject)
它把所有观察者对象的引用保存到一个集合里,每个主题都可以有任何数量的观察者。抽象主题提供一个接口,可以增加和删除观察者对象。
具体主题(ConcreteSubject)
将有关状态存入具体观察者对象;在具体主题内部状态改变时,给所有登记过的观察者发出通知。
抽象观察者(Observer)
为所有的具体观察者定义一个接口,在得到主题通知时更新自己。
具体观察者(ConcreteObserver)
实现抽象观察者角色所要求的更新接口,以便使本身的状态与主题状态协调。
java代码
抽象(接口主题(发布者))
package com.jlife.pattern.subjecobserver;
/**
* Created by chen on 2017/3/9.
* <p>
* Email 122741482@qq.com
* <p>
* Describe: 主题抽象类(接口)
*/
public interface Subject {
/**
* 添加观察者
* @param oberver
*/
void addObserver(Oberver oberver);
/**
* 移除观察者
* @param oberver
*/
void removeObserver(Oberver oberver);
/**
* 发送消息
* @param msg
*/
void sendMessage(String msg);
}
具体主题(发布者)
package com.jlife.pattern.subjecobserver;
import java.util.ArrayList;
import java.util.List;
/**
* Created by chen on 2017/3/9.
* <p>
* Email 122741482@qq.com
* <p>
* Describe: 主题实现类
*/
public class SubjectImpl implements Subject {
private List<Oberver> obervers = new ArrayList<Oberver>();
@Override
public void addObserver(Oberver oberver) {
obervers.add(oberver);
}
@Override
public void removeObserver(Oberver oberver) {
obervers.remove(oberver);
}
@Override
public void sendMessage(String msg) {
if (obervers == null || obervers.size() == 0) {
return;
}
for (Oberver oberver : obervers) {
oberver.update(msg);
}
}
}
抽象接口(订阅者,观察者)
package com.jlife.pattern.subjecobserver;
/**
* Created by chen on 2017/3/9.
* <p>
* Email 122741482@qq.com
* <p>
* Describe: 订阅者抽象类(接口)
*/
public interface Oberver {
void update(Object msg);
}
具体(订阅者,观察者)
package com.jlife.pattern.subjecobserver;
/**
* Created by chen on 2017/3/9.
* <p>
* Email 122741482@qq.com
* <p>
* Describe:
*/
public class OberverImpl implements Oberver {
private String name;
public OberverImpl(String name,Subject subject) {
this(subject);
this.name = name;
}
public OberverImpl(Subject subject) {
super();
subject.addObserver(this);
}
public OberverImpl(String name) {
super();
this.name = name;
}
@Override
public void update(Object msg) {
System.out.println(this.name + "============" + msg);
}
}
测试类
package com.jlife.test.pattern;
import com.jlife.pattern.subjecobserver.Oberver;
import com.jlife.pattern.subjecobserver.OberverImpl;
import com.jlife.pattern.subjecobserver.Subject;
import com.jlife.pattern.subjecobserver.SubjectImpl;
import org.junit.Test;
/**
* Created by chen on 2017/3/9.
* <p>
* Email 122741482@qq.com
* <p>
* Describe:
*/
public class SubjectOberver {
@Test
public void test() {
Subject subject = new SubjectImpl();
Oberver o1 = new OberverImpl("1");
Oberver o2 = new OberverImpl("2");
Oberver o3 = new OberverImpl("3");
Oberver o4 = new OberverImpl("4");
Oberver o5 = new OberverImpl("5");
subject.addObserver(o1);
subject.addObserver(o2);
subject.addObserver(o3);
subject.addObserver(o4);
subject.addObserver(o5);
subject.sendMessage("woc");
subject.removeObserver(o3);
subject.sendMessage("nb");
Subject subject1 = new SubjectImpl();
Oberver o6 = new OberverImpl(subject1);
Oberver o7 = new OberverImpl(subject1);
Oberver o8 = new OberverImpl(subject1);
Oberver o9 = new OberverImpl(subject1);
Oberver o10 = new OberverImpl(subject1);
subject1.sendMessage("nnnn");
Subject subject3 = new SubjectImpl();
Oberver o11 = new OberverImpl("o11",subject3);
Oberver o12 = new OberverImpl("o12",subject3);
Oberver o13 = new OberverImpl("o13",subject3);
Oberver o14 = new OberverImpl("o14",subject3);
Oberver o15 = new OberverImpl("o15",subject3);
subject3.sendMessage("sasdsad");
}
}
运行结果
1============woc
2============woc
3============woc
4============woc
5============woc
1============nb
2============nb
4============nb
5============nb
null============nnnn
null============nnnn
null============nnnn
null============nnnn
null============nnnn
o11============sasdsad
o12============sasdsad
o13============sasdsad
o14============sasdsad
o15============sasdsad
我的官网
我的官网http://guan2ye.com
我的CSDN地址http://blog.csdn.net/chenjianandiyi
我的简书地址http://www.jianshu.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的码云地址https://gitee.com/jamen/
阿里云优惠券https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=vf2b5zld&utm_source=vf2b5zld
阿里云教程系列网站http://aliyun.guan2ye.com
我的开源项目spring boot 搭建的一个企业级快速开发脚手架
时间: 2024-10-29 19:25:03