⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chainofresponsibility.htm

📁 DesignPattern基于Java方面最好的书
💻 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:&nbsp;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;">&nbsp;&nbsp;&nbsp; // 执行请求一 </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;">&nbsp;&nbsp;&nbsp; // 执行请求二 </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;">&nbsp;&nbsp;&nbsp; // 执行预设请求或显示讯息</span><br>
</div>
&nbsp;<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 + -