?? chainofresponsibility.htm
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="stylesheet" href="css/stdlayout.css" type="text/css">
<link rel="stylesheet" href="css/print.css" type="text/css">
<meta content="text/html; charset=gb2312" http-equiv="content-type">
<title>Chain of Responsibility 模式</title>
</head>
<body>
<h3><a href="http://caterpillar.onlyfun.net/GossipCN/index.html">From
Gossip@caterpillar</a></h3>
<h1><a href="CppGossip.html">Design Pattern: Chain of Responsibility 模式</a></h1>
其實Chain of Responsibility的概念,即使是一個剛學程式設計的新手也會用到,一個簡單的 if...else if ... else 流程控制就有Chain of Responsibility的概念:<br>
<div style="margin-left: 40px;"><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">if(/* 符合請求條件一 */) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> // 執行請求一 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else if(/* 符合請求條件二 */) </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> // 執行請求二 </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;">else </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;">
<span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> // 執行預設請求或顯示訊息</span><br>
</div>
<br>
這是從結構化程式設計的觀點來看Chain of Responsibility的概念,若使用物件的觀點來看Chain of
Responsibility的話,有一個較佳的例子就是Java的例外處理機制,當程式中發生例外時,也比會catch所捕捉的例外是否符合,如果符合
就執行所設定的處理,如果都沒有比對到適當的例外物件,就會將例外丟出try...catch區塊之外。<br>
<br>
在 <a href="GoF.htm">Gof 的書</a> 中給定Chain of Responsibility目的為:使多個物件都有機會處理請求,以避免請求的發送者與接收者之間的耦合關系,將這些物件組合為一個鏈,并沿著這個鏈傳遞該請求,直到有物件處理它為止。<br>
<br>
先用一個例子來說明使用if...else的方式來處理請求: <br>
<ul>
<li> IHandler.java
</li>
</ul>
<pre>public interface IHandler {<br> public void handle(); <br>} <br></pre>
<br>
<ul>
<li> SymbolHandler.java
</li>
</ul>
<pre>public class SymbolHandler implements IHandler { <br> public void handle() { <br> System.out.println("Symbol has been handled"); <br> } <br>} <br></pre>
<br>
<ul>
<li> CharacterHandler.java
</li>
</ul>
<pre>public class CharacterHandler implements IHandler { <br> public void handle() { <br> System.out.println("Character has been handled"); <br> } <br>} <br></pre>
<br>
<ul>
<li> NumberHandler.java
</li>
</ul>
<pre>public class NumberHandler implements IHandler { <br> public void handle() { <br> System.out.println("Number has been handled"); <br> } <br>} <br></pre>
<br>
<ul>
<li> Application.java
</li>
</ul>
<pre>import java.io.*; <br><br>public class Application { <br> public void run() throws Exception { <br> System.out.print("Press any key then return: "); <br> char c = (char) System.in.read(); <br><br> IHandler handler = null; <br> if (Character.isLetter(c)) {<br> handler = new CharacterHandler(); <br> }<br> else if (Character.isDigit(c)) {<br> handler = new NumberHandler(); <br> }<br> else {<br> handler = new SymbolHandler(); <br> }<br><br> handler.handle(); <br> } <br><br> public static void main(String[] args) <br> throws IOException {<br> Application app = new Application();<br> app.run(); <br> } <br>} <br></pre>
<br>
這是一個很簡單的程式,可以判定您所輸入的是數字、字元或是符號,如果將之以物件的方式來組織物件之間的職責,可以將程式改寫如下:<br>
<ul>
<li> Handler.java
</li>
</ul>
<pre>public class Handler { <br> private Handler successor;<br><br> public void setSuccessor(Handler successor) { <br> this.successor = successor; <br> }<br><br> public Handler getSuccessor() { <br> return successor; <br> }<br><br> public void handleRequest(char c) { <br> if(successor != null) <br> successor.handleRequest(c); <br> } <br>} <br></pre>
<br>
<ul>
<li> NumberHandler.java
</li>
</ul>
<pre>public class NumberHandler extends Handler { <br> public void handleRequest(char c) { <br> if(Character.isDigit(c)) { <br> System.out.println("Number has been handled"); <br> } <br> else {<br> getSuccessor().handleRequest(c); <br> }<br> } <br>} <br></pre>
<br>
<ul>
<li> CharacterHandler.java
</li>
</ul>
<pre>public class CharacterHandler extends Handler { <br> public void handleRequest(char c) { <br> if(Character.isLetter(c)) { <br> System.out.println("Character has been handled"); <br> } <br> else {<br> getSuccessor().handleRequest(c); <br> }<br> } <br>} <br></pre>
<br>
<ul>
<li> SymbolHandler.java
</li>
</ul>
<pre>public class SymbolHandler extends Handler { <br> public void handleRequest(char c) { <br> System.out.println("Symbol has been handled"); <br> } <br>} <br></pre>
<br>
<ul>
<li> Application.java
</li>
</ul>
<pre>import java.io.*; <br><br>public class Application {<br> public static void main(String[] args) <br> throws IOException { <br> Handler numberHandler = new NumberHandler(); <br> Handler characterHandler = new CharacterHandler(); <br> Handler symbolHandler = new SymbolHandler(); <br><br> numberHandler.setSuccessor(characterHandler); <br> characterHandler.setSuccessor(symbolHandler); <br><br> System.out.print("Press any key then return: "); <br> char c = (char)System.in.read(); <br> numberHandler.handleRequest(c); <br> } <br>} </pre>
<br>
在組織物件之間的職責時,通常是從細粒度至粗粒度的方式來組織,從特殊到抽象化,就像程式中將數字視為字元的特殊化,字元又為符號的特殊化。 <br>
<br>
Chain of Responsibility的 UML 結構圖如下所示: <br>
<div style="text-align: center;"><img style="width: 372px; height: 217px;" alt="Chain of Responsibility" title="Chain of Responsibility" src="images/chainOfResponsibility-1.jpg"><br>
</div>
<br>
從物件執行請求的時間來看,其運作是很簡單的職責傳遞而已,如下:<br>
<div style="text-align: center;"><img style="width: 370px; height: 258px;" alt="Chain of Responsibility" title="Chain of Responsibility" src="images/chainOfResponsibility-2.jpg"><br>
</div>
<br>
以上所舉的例子在請求上是很簡單的,只是比對輸入的型態,在更一般的情況下,可以將請求包裝為一個物件,并提供getType()之間的方法,以讓
Chain of Responsibility中的物件進行比對,例如: <br>
<ul>
<li>Request.java</li>
</ul>
<pre>public class Request{ <br> private String type; <br><br> public Request(String type) { this.type=type; }<br> public String getType() { return type; }<br><br> public void execute(){ <br> // 執行請求 <br> } <br>} <br></pre>
<br>
在Gof的書中所舉的例子為輔助說明系統,在一個介面中希望使用者一定可以得到相關的說明主題,如果子元件有說明的話,就顯示相關說明,否則的話就轉發給
包括它的容器元件或父元件,以保證使用者的輔助說明請求一定可以得到回應。<br>
<br>
<br>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -