分类: Java
2011-06-01 21:15:27
观察者模式:
定义了对象之间的一对多依赖,这样以来,当一个对象改变状态时,它的所有以来者都会收到通知并自动更新。(联想 报纸订阅 报社 -- 读者)
设计原则
为了交互对象之间的松耦合设计而努力
多用组合 少用继承
类介绍:
接口
Observer -- 观察者
Subject -- 主题
DisplayElement -- 显示接口
实现类
PostOffice -- 邮局
Subscriber -- 订阅者
source code:
package com.bbsoft.observer.test;
/**
*
* @author bbsoft
*
*/
public interface Subject {
/**
* @param o
* - register observer
*/
void registerObserver(Observer o);
/**
* @param o
* - remove observer
*/
void removeObserver(Observer o);
/**
* notify Observers
*/
void notifyObservers();
}
package com.bbsoft.observer.test;
/**
*
* @author bbsoft
*
*/
public interface Observer {
/**
*
* @param magazineName
* - magazine Name
* @param publishDate
* - publish date
*/
void update(String magazineName, String publishDate);
}
package com.bbsoft.observer.test;
/**
*
* @author bbsoft
*
*/
public interface DisplayElement {
/**
* display the information
*/
void display();
}
package com.bbsoft.observer.test;
import java.util.ArrayList;
import java.util.Iterator;
/**
*
* @author bbsoft
*
*/
public class Postoffice implements Subject {
private ArrayList observers;
private String magazineName;
private String publishDate;
/**
* public constructor
*/
public Postoffice() {
super();
this.observers = new ArrayList();
}
/**
* notify Observers
*/
public void notifyObservers() {
Iterator observerIt = observers.iterator();
while (observerIt.hasNext()) {
Observer observer = (Observer) observerIt.next();
observer.update(magazineName, publishDate);
}
}
/**
* register Observer
*
* @param o
* - observer
*/
public void registerObserver(Observer o) {
this.observers.add(o);
}
/**
* remove Observer
*
* @param o
* - observer
*/
public void removeObserver(Observer o) {
boolean isExist = observers.contains(o);
if (isExist) {
this.observers.remove(o);
} else {
System.out.println("observers don't have the object which you input , can not remove");
}
}
/**
*
* @param magazineName
* - magazine Name
* @param publishDate
* - publish Date
*/
public void publishMagazine(String magazineName, String publishDate) {
this.magazineName = magazineName;
this.publishDate = publishDate;
notifyObservers();
}
}
package com.bbsoft.observer.test;
/**
*
* @author bbsoft
*
*/
public class Subscriber implements Observer, DisplayElement {
private String magazineName;
private String publishDate;
private String subscriberName;
/**
*
* public constructor
*
* @param subscriberName
* - subscriber Name
*/
public Subscriber(String subscriberName) {
this.subscriberName = subscriberName;
}
/**
* display the information
*/
public void display() {
System.out.println("subscriber Name: " + subscriberName);
System.out.println("magazine Name: " + magazineName);
System.out.println("publish Date: " + publishDate);
System.out.println("------------------------------");
}
/**
* @param magazineName
* - magazine Name
*
* @param publishDate
* - publish Date
*
*
*/
public void update(String magazineName, String publishDate) {
this.magazineName = magazineName;
this.publishDate = publishDate;
display();
}
}
测试类
package com.bbsoft.observer.test;
public class TestObserver {
public static void main(String[] args) {
Postoffice post = new Postoffice();
Subscriber tom = new Subscriber("Tom");
Subscriber barry = new Subscriber("Barry");
post.registerObserver(tom);
post.registerObserver(barry);
post.publishMagazine("Advanced", "2009-06-30");
post.removeObserver(tom);
post.publishMagazine("Business", "2009-06-31");
}
}