觀察者模式定義了一個一對多的對象關系
本文將給出一個相應的事例具體說明觀察者模式是如果工作的
先定義兩個接口
/** *//**
* 主體對象接口定義了注冊觀察者
*
*/
public interface ISubject {
/** *//**
* 為該主體對象注冊一個觀察者
*/
public void registerObserver(IObserver observer);
/** *//**
* 從該主體對象中取消一個觀察者的注冊
*/
public void removeObserver(IObserver observer);
/** *//**
* 通知所有觀察者
*/
public void notifyObserver();
}
/** *//**
* 觀察者接口簡單定義了一個用來更新觀察者信息的接口
* 當主體對象被更新時
*/
public interface IObserver {
/** *//**
* 接口方法用來更新觀察者的信息
*/
public void remind(ITask taskSubject);
}
這兩個接口只定義了主題對象和觀察者對象所需要的接口
/** *//**
* 這裡定義了一個任務應該具有的功能
*/
public interface ITask {
public void setStatus(String status);
public String getStatus();
public void setProcess(String process);
public String getProcess();
public void setCheckList(String checkList);
public String getCheckList();
}
然後我們創建具體的任務主體
import java
import java
public class TaskSubject implements ISubject
// 在這裡對觀察者列表進行初始化
static {
_observers = new ArrayList<IObserver>();
}
@Override
public void notifyObserver() {
// 調用觀察者的方法通知並更新觀察者信息
for(IObserver observer : _observers) {
observer
}
}
@Override
public void registerObserver(IObserver observer) {
if(ntains(observer)) {
System
}
// Register an observer
_observers
System
}
@Override
public void removeObserver(IObserver observer) {
if(!ntains(observer)) {
System
}
// Remove an observer
_observers
System
}
@Override
public String getCheckList() {
return this
}
@Override
public String getProcess() {
return this
}
@Override
public String getStatus() {
return this
}
@Override
public void setCheckList(String checkList) {
this
}
@Override
public void setProcess(String process) {
this
}
@Override
public void setStatus(String status) {
this
}
// 這裡將觀察者列表定義為一個靜態的變量
// 這裡用到了泛型
private static List<IObserver> _observers;
private String _status;
private String _process;
private String _checkList;
}
在這裡我們沒有給觀察者定義接口
/** *//**
* 這個抽象類繼承了Iobserver接口
* 在remind方法中從主體對象中獲取所有需要的信息
* 並調用sendEmail方法對觀察者實時進行更新
*/
public abstract class TaskObserver implements IObserver {
@Override
public void remind(ITask taskSubject) {
this
this
this
// 更新觀察者對象
this
}
public abstract void sendEmail();
// 工具類方法
public void print(String msg) {
System
}
// 在父類中定義參數
protected String _status;
protected String _process;
protected String _checkList;
}
然後定義任務受托人[assignee]
public class Assignee extends TaskObserver {
@Override
public void sendEmail() {
print(
print(
print(
}
public String toString() {
return
}
}
public class Reviewer extends TaskObserver {
@Override
public void sendEmail() {
print(
print(
print(
}
public String toString() {
return
}
}
public class Reporter extends TaskObserver {
@Override
public void sendEmail() {
print(
print(
print(
}
public String toString() {
return
}
}
然後我們需要編寫一個類用來演示觀察者模式
public class TaskManager {
public static void main(String[] args) {
// Create subject
TaskSubject taskSubject = new TaskSubject();
// Create observers
IObserver assignee = new Assignee();
IObserver reviewer = new Reviewer();
IObserver reporter = new Reporter();
// 注冊觀察者[因為我們使用的是一個列表
taskSubject
taskSubject
taskSubject
// 更新主體對象的信息
taskSubject
taskSubject
taskSubject
// 通知所有觀察者
taskSubject
// 更新主體對象信息
taskSubject
taskSubject
taskSubject
// 取消報告者的注冊
taskSubject
taskSubject
}
}
輸出的信息如下
< Assignee > is registed successfully!
< Reviewer > is registed successfully!
< Reporter > is registed successfully!
[Assignee] The current status is : <Assigned>
[Assignee] The current process is : <No Process Attacted>
[Assignee] The current checklist is : <CheckList Version
[Reviewer] The current status is : <Assigned>
[Reviewer] The current process is : <No Process Attacted>
[Reviewer] The current checklist is : <CheckList Version
[Reporter] The current status is : <Assigned>
[Reporter] The current process is : <No Process Attacted>
[Reporter] The current checklist is : <CheckList Version
< Reporter > is removed successfully!
[Assignee] The current status is : <In Progress>
[Assignee] The current process is : <Process Attached>
[Assignee] The current checklist is : <CheckList Version Final Version>
[Reviewer] The current status is : <In Progress>
[Reviewer] The current process is : <Process Attached>
[Reviewer] The current checklist is : <CheckList Version Final Version>
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27595.html