观察者模式

来源:互联网 发布: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);


}

——————————————————————————————————————————————————————————————————————————————————————————————



0 0