观察者模式
来源:互联网 发布:400软件 编辑:程序博客网 时间:2024/05/14 20:49
观察者模式适合使用在对某一个状态或者事件的监听,更新其所有的订阅者的状态
package com.test.observer;
/**
* 发布
* @author
*
*/
public abstract class Publish {// 也可以使用接口作为定义
public abstract void doSendingNewsPaper();// 更新发送报纸
}
——————————————————————————————————————————————————————
package com.test.observer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 具体需要观察的对象,当中的doSendingNewsPaper方法即为订阅者所观察的状态,当这个被调用,就会更新所有订阅的状态。
* @author
*
*/
public class NewsPaperPublish extends Publish{
private List<SubScribe> subScribes;
/**
* 需要观察的状态,本例中为发送报纸
*/
@Override
public void doSendingNewsPaper() {
// TODO Auto-generated method stub
Iterator<SubScribe> iterable = subScribes.iterator();
while (iterable.hasNext()) {
SubScribe subScribe = iterable.next();
//if (!subScribe.getAcceptNewsPaperStatus()){
String name = ((SubScribeCustomer) subScribe).getStrName();
subScribe.doAcceptingNewsPaper(name);
// subScribe.setAcceptNewsPaperStatus(true);
//}
}
}
public void registeSubscriber(SubScribe mSubScribe){
subScribes.add(mSubScribe);
if (mSubScribe instanceof SubScribeCustomer){
System.out.println(((SubScribeCustomer)mSubScribe).getStrName() + "于" + System.currentTimeMillis()+ "订阅报纸");
}
}
public void removeSubscriber(int index){
subScribes.remove(index);
}
private NewsPaperPublish(){
subScribes = new ArrayList<>();
}
public static NewsPaperPublish newInstance(){
return NewsPaperPublishProduce.INSTANCE;
}
private static class NewsPaperPublishProduce{
public static final NewsPaperPublish INSTANCE = new NewsPaperPublish();
}
}
————————————————————————————————————————————————————————————————————————————————————————————————————————————
package com.test.observer;
/**
* 订阅
* @author
*
*/
public abstract class SubScribe {// 也可以使用接口
public abstract void doAcceptingNewsPaper(String subscribeNewsPaperName);// 收新报纸
public abstract Boolean getAcceptNewsPaperStatus();// 收报纸的状态
public abstract void setAcceptNewsPaperStatus(Boolean arg0);// 收报纸的状态
}
————————————————————————————————————————————————————————————————————————————————————————————————————————————
package com.test.observer;
import java.util.HashMap;
import java.util.Map;
/**
* 具体的订阅者
* @author
*
*/
public class SubScribeCustomer extends SubScribe{
private String strName;
private Boolean status = false;
private Map<String, Boolean> mapStatus = new HashMap<String, Boolean>();
public SubScribeCustomer() {
super();
// TODO Auto-generated constructor stub
}
public SubScribeCustomer(String strName) {
super();
// TODO Auto-generated constructor stub
this.strName = strName;
}
public String getStrName(){
return strName;
}
@Override
public void doAcceptingNewsPaper(String subscribeNewsPaperName) {
// TODO Auto-generated method stub
if (mapStatus.get(subscribeNewsPaperName) == null ? true : false){
System.out.println("当前有新的报纸发出," + strName + "可以接收新的报纸");
mapStatus.put(subscribeNewsPaperName, true);
} else {
System.out.println("当前有新的报纸发出," + strName + "已经接收过该报纸");
}
}
@Override
public Boolean getAcceptNewsPaperStatus() {
// TODO Auto-generated method stub
return status;
}
@Override
public void setAcceptNewsPaperStatus(Boolean arg0) {
// TODO Auto-generated method stub
status = arg0;
}
}
——————————————————————————————————————————————————————————————————————————————————————————————————————
public static void main(String[] args){
System.out.println("________________________________________");
SubScribe customer0 = new SubScribeCustomer("顾客1号");
SubScribe customer1 = new SubScribeCustomer("顾客2号");
SubScribe customer2 = new SubScribeCustomer("顾客3号");
//Publish newsPublish = NewsPaperPublish.newInstance();
final NewsPaperPublish newsPaperPublish = NewsPaperPublish.newInstance();
newsPaperPublish.registeSubscriber(customer0);
newsPaperPublish.registeSubscriber(customer1);
newsPaperPublish.registeSubscriber(customer2);
newsPaperPublish.doSendingNewsPaper();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
SubScribe customer3 = new SubScribeCustomer("顾客4号");
newsPaperPublish.registeSubscriber(customer3);
newsPaperPublish.doSendingNewsPaper();
}
}, 1000);
}
package com.test.observer;
/**
* 发布
* @author
*
*/
public abstract class Publish {// 也可以使用接口作为定义
public abstract void doSendingNewsPaper();// 更新发送报纸
}
——————————————————————————————————————————————————————
package com.test.observer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* 具体需要观察的对象,当中的doSendingNewsPaper方法即为订阅者所观察的状态,当这个被调用,就会更新所有订阅的状态。
* @author
*
*/
public class NewsPaperPublish extends Publish{
private List<SubScribe> subScribes;
/**
* 需要观察的状态,本例中为发送报纸
*/
@Override
public void doSendingNewsPaper() {
// TODO Auto-generated method stub
Iterator<SubScribe> iterable = subScribes.iterator();
while (iterable.hasNext()) {
SubScribe subScribe = iterable.next();
//if (!subScribe.getAcceptNewsPaperStatus()){
String name = ((SubScribeCustomer) subScribe).getStrName();
subScribe.doAcceptingNewsPaper(name);
// subScribe.setAcceptNewsPaperStatus(true);
//}
}
}
public void registeSubscriber(SubScribe mSubScribe){
subScribes.add(mSubScribe);
if (mSubScribe instanceof SubScribeCustomer){
System.out.println(((SubScribeCustomer)mSubScribe).getStrName() + "于" + System.currentTimeMillis()+ "订阅报纸");
}
}
public void removeSubscriber(int index){
subScribes.remove(index);
}
private NewsPaperPublish(){
subScribes = new ArrayList<>();
}
public static NewsPaperPublish newInstance(){
return NewsPaperPublishProduce.INSTANCE;
}
private static class NewsPaperPublishProduce{
public static final NewsPaperPublish INSTANCE = new NewsPaperPublish();
}
}
————————————————————————————————————————————————————————————————————————————————————————————————————————————
package com.test.observer;
/**
* 订阅
* @author
*
*/
public abstract class SubScribe {// 也可以使用接口
public abstract void doAcceptingNewsPaper(String subscribeNewsPaperName);// 收新报纸
public abstract Boolean getAcceptNewsPaperStatus();// 收报纸的状态
public abstract void setAcceptNewsPaperStatus(Boolean arg0);// 收报纸的状态
}
————————————————————————————————————————————————————————————————————————————————————————————————————————————
package com.test.observer;
import java.util.HashMap;
import java.util.Map;
/**
* 具体的订阅者
* @author
*
*/
public class SubScribeCustomer extends SubScribe{
private String strName;
private Boolean status = false;
private Map<String, Boolean> mapStatus = new HashMap<String, Boolean>();
public SubScribeCustomer() {
super();
// TODO Auto-generated constructor stub
}
public SubScribeCustomer(String strName) {
super();
// TODO Auto-generated constructor stub
this.strName = strName;
}
public String getStrName(){
return strName;
}
@Override
public void doAcceptingNewsPaper(String subscribeNewsPaperName) {
// TODO Auto-generated method stub
if (mapStatus.get(subscribeNewsPaperName) == null ? true : false){
System.out.println("当前有新的报纸发出," + strName + "可以接收新的报纸");
mapStatus.put(subscribeNewsPaperName, true);
} else {
System.out.println("当前有新的报纸发出," + strName + "已经接收过该报纸");
}
}
@Override
public Boolean getAcceptNewsPaperStatus() {
// TODO Auto-generated method stub
return status;
}
@Override
public void setAcceptNewsPaperStatus(Boolean arg0) {
// TODO Auto-generated method stub
status = arg0;
}
}
——————————————————————————————————————————————————————————————————————————————————————————————————————
public static void main(String[] args){
System.out.println("________________________________________");
SubScribe customer0 = new SubScribeCustomer("顾客1号");
SubScribe customer1 = new SubScribeCustomer("顾客2号");
SubScribe customer2 = new SubScribeCustomer("顾客3号");
//Publish newsPublish = NewsPaperPublish.newInstance();
final NewsPaperPublish newsPaperPublish = NewsPaperPublish.newInstance();
newsPaperPublish.registeSubscriber(customer0);
newsPaperPublish.registeSubscriber(customer1);
newsPaperPublish.registeSubscriber(customer2);
newsPaperPublish.doSendingNewsPaper();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
SubScribe customer3 = new SubScribeCustomer("顾客4号");
newsPaperPublish.registeSubscriber(customer3);
newsPaperPublish.doSendingNewsPaper();
}
}, 1000);
}
——————————————————————————————————————————————————————————————————————————————————————————————
0 0
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- 观察者模式
- NYOJ:挑战密室
- Android下的Nine-Patch图片制作总结
- 二分搜索学习笔记 Java实现
- php发送邮件email
- eclipse中maven项目部署到tomcat
- 观察者模式
- 优雅编程之这样重构对象,你就“正常”了(二十一)
- PAT 1014. Waiting in Line (30)
- 自定义下拉刷新控件 - RefreshableView(支持所有控件的下拉刷新)
- html5解决大文件断点续传
- css3垂直居中的几种方法
- web学习
- CodeForces 282CXOR and OR
- Python 元组、集合和字典 实例