定义:
观察者模式定义了一种“一对多”的依赖关系,让多个观察着对象同时监听一个主题对象,当这个主题对象的状态发生变化时,它会通知所有的观察者对象,使这些观察者对象能够自动更新各自的行为或者状态!
Java平台实现观察者模式:
java.util.Observable类:
主题对象继承这个类,主题对象可以有多个观察者对象,主题对象发生该变后,需要调用notifyObservers方法来通知它的所有观察者发生了改变(java平台系统调用各个观察者对象的update方法)
java.util.Observer
观察者实现这个接口,并通过update方法实现对各个观察者状态的更新和该变!
观察者模式在网上商城系统中应用的例子:
主题对象:
public class GoodsInfo extends Observable{ private String goodName; private float goodPrice; public GoodsInfo(){ } public String getGoodName(){ return goodName; } public void setGoodName(String newGoodName){ this.goodName=newGoodName; this.setChanged();//设置变化点 notifyObservers(goodName);//当信息发生变化时将通知各个观察者 } public float getGoodPrice(){ return goodPrice; } public void setGoodPrice(float newGoodPrice){ this.goodPrice=newGoodPrice; this.setChanged(); notifyObservers(new Float(goodPrice)); } }
|
新商品观察者:
public class NewGoodUserObserver implements Oberver{ private String newGoodName=null; public NewGoodUserInfo{ } public void update(Observer obj//主题对象,Object arg//改变的状态名称){ if(arg instanceof String){ newGoodName=(String)arg; System.out.println("商品的名称已经改变为:"+newGoodName); } } }
|
商品降价观察者:
public class GoodPriceOffUserObserve implements Oberver{ private float newGoodPrice=null; public NewGoodUserInfo{ } public void update(Observer obj,Object arg){ if(arg instanceof Float){ newGoodPrice=((Float)arg).floatValue(); System.out.println("商品的价格已经改变为:"+newGoodPrice); } } }
|
商品管理员类:
public class GoodsInfoManager{ public GoodsInfoManager(){ } public void doUpdateGoodsInfo(){ GoodsInfo oneGoodsInfo=new GoodsInfo(); NewGoodsUserObserver newGoodsUserObserver=new NewGoodsUserObserver(); GoodsPriceOffUserObserver goodsPriceOffUserObserver=new GoodsPriceOffUserObserver(); oneGoodsInfo.addObserver(newGoodsUserObserver); oneGoodsInfo.addObserver(goodsPriceOffUserObserver); oneGoodsInfo.setGoodName("J2EE设计模式"); oneGoodsInfo.setGoodPrice(49.50f); } public static void main(String args[]){ GoodsInfoManager oneGoodsInfoManager=new GoodsInfoManager(); oneGoodsInfoManager.doUpdateGoodsInfo(); } }
|
观察者模式在BBS系统中应用的例子(给新注册的用户发送一封邮件):
代表用户信息的类(主题)
public class UserInfoManager extends Observable implements UserInfoInterface{
SendMailBean oneSendMailBean=null;
public UserInfoManager(){
oneSendMailBean=new SendMailBean();
this.addObserver(oneSendMailBean);
}
public boolean doUpdateUserInfo(UserInfoVO oneUserInfoVO){
return false;
}
public boolean doUserLogin(UserInfoVO oneUserInfoVO){
return false;
}
public boolean doUserRegister(UserInfoVO oneUserInfoVO){
boolean registerResult=true;
//完成成功的具体实现
this.setChanaged();
notifiObservers(oneUserInfoVO);
return registerResult;
}
}
观察者(发送邮件):
public class SendMailBean implements SendMailInterface,Observer{
public SendMailBean(){
}
public boolean doSendMail(UserInfoVO oneUserInfoVO){
boolean rsendMailResult=true;
String userName=oneUserInfoVO.getUserName();
String userPassword=oneUserInfoVO.getUserPassword();
String userIDCardInfo=oneUserInfoVO.getIDCardInfo();
System.out.println("已经向您的邮箱发送了邮件,用户名称为"+userName);
return rsendMailResult;
}
public void update(Observable obj,Object arg){
if((obj instanceof UserInfoManager)&&(arg instanceof UserInfoVO)){
UserInfoVO oneUserInfoVO=(UserInfoVO)arg;
doSendMail(oneUserInfoVO);
}
}
}
可以对UserInfoManager类进行单元测试
阅读(995) | 评论(0) | 转发(0) |