中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 设计模式
[Behavioral Patterns] Chain of Responsibility
作者:未知 时间:2005-07-24 21:23 出处:JR 责编:chinaitpower
              摘要:[Behavioral Patterns] Chain of Responsibility

 Intent 


Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. 

 Problem 


There is a potentially variable number of "handler" objects and a stream of requests that must be handled. Need to efficiently process the requests without hard-wiring handler relationships and precedence, or request-to-handler mappings. 

 Structure 


Steps are as follow:

1. Base class maintains a "next" pointer
2. Each "link" does its part to handle (and/or pass on) the request
3. Client "launches and leaves" each request with the chain
4. The current bid and bidder are maintained in chain static members
5. The last link in the chain assigns the job to the low bidder

  1. /**
  2.  * This class is the abstract class from which all elements in the 
  3.  * chain inherit. 
  4.  */
  5. public abstract class Handler 
  6.     //HAS: 
  7.     //Each element in the chain has a title (such as secretary), a 
  8.     //superior (such as Governer), and a beurocratic response to use 
  9.     //in responding to a letter that it handles. 
  10.     Handler superior = null
  11.     String title; 
  12.     //note the irony of the static nature of the beurocratic response. 
  13.     static final String beurocraticResponse = "Thank you for your letter. It's not our fault. Send us more money.  Sincerely, Governer Taft."
  14.     
  15.     //There is no CONSTRUCTOR becuase the class is abstract. 
  16.     
  17.     //METHODS: 
  18.     //This method changes from chain element to chain element 
  19.     //because each element uses different criteria to evaluate 
  20.     //whether the letter is handleable and there is no method body 
  21.     //filled in in the abstract class. 
  22.     abstract boolean canHandle(Letter kvetch); 
  23.     
  24.     //This method remains constant for all chain elements, and so can 
  25.     //be written here once and for all. 
  26.     public Letter handleLetter(Letter kvetch) 
  27.     { 
  28.         //if the element in the chain can handle the letter, 
  29.         if(this.canHandle(kvetch)) 
  30.         { 
  31.         //Send a reply. 
  32.         return new Letter(kvetch.getSender(), "Governor Taft", beurocraticResponse, kvetch.getSubject());
  33.         } 
  34.         
  35.         //otherwise, it becomes my bosses problem. 
  36.         else if(superior != null
  37.         { 
  38.         return superior.handleLetter(kvetch);
  39.         } 
  40.         //The Governor doesn't have a boss whose problem it can 
  41.         //become, so just in case, we account for this exception. 
  42.         throw new java.lang.RuntimeException("Invalid Recipient -- No one can handle this problem.");
  43.     }
  44.   
  45. public class Letter extends Object 
  46.     //HAS: 
  47.     //The letter has four parts (four Strings): A string to represent 
  48.     //the recipient, sender, content of the letter (text), and subject 
  49.     //of the letter. Each object in the chain uses the subejct string 
  50.     //to determine whether it can handle the letter. 
  51.     String recipient = new String(""); 
  52.     String sender = new String(""); 
  53.     String text = new String(""); 
  54.     String subject = new String(""); 
  55.     
  56.     //CONSTRUCTOR 
  57.     public Letter(String isTo, String isFrom, String contents, String topic) 
  58.     { 
  59.         //Always a good idea to call the super's constructor. 
  60.         super(); 
  61.         //initializations for the letter happen in its constructor. 
  62.         setRecipient(isTo); 
  63.         setSender(isFrom); 
  64.         setText(contents); 
  65.         setSubject(topic);
  66.     } 
  67.     
  68.     //Accessor METHODS for each of the four strings. 
  69.     public String getRecipient() 
  70.     { 
  71.         return this.recipient;
  72.     } 
  73.     public String getSender() 
  74.     { 
  75.         return this.sender;
  76.     } 
  77.     public String getText() 
  78.     { 
  79.         return this.text;
  80.     } 
  81.     public String getSubject() 
  82.     { 
  83.         return this.subject;
  84.     } 
  85.     private void setRecipient(String isTo) 
  86.     { 
  87.         this.recipient = isTo;
  88.     } 
  89.     
  90.     private void setSender(String isFrom) 
  91.     { 
  92.         this.sender = isFrom;
  93.     } 
  94.     private void setText(String contents) 
  95.     { 
  96.         this.text = contents;
  97.     } 
  98.     private void setSubject(String topic) 
  99.     { 
  100.         this.subject = topic;
  101.     } 
  102.     //Overriden toString METHOD so that we can use letters as the selected items in the combobox. 
  103.     public String toString() 
  104.     { 
  105.         return this.getSubject();
  106.     }
  107. public class Governer extends Handler 
  108.     //CONSTRUCTOR  for Governor is different from all the other 
  109.     //handlers because it doesn't have a "boss" and thus its superior is 
  110.     //set to null. 
  111.     public Governer() 
  112.     { 
  113.         super(); 
  114.         this.superior= null
  115.         this.title = "Governer";
  116.     } 
  117.     //METHOD 
  118.     public boolean canHandle(Letter kvetch) 
  119.     { 
  120.         //The Governer only handles donations. 
  121.         if(kvetch.getSubject().equalsIgnoreCase("Donation")) return true
  122.         else return false;
  123.     }
  124. public class Secretary extends Handler 
  125.     //CONSTRUCTOR 
  126.     public Secretary(Handler boss) 
  127.     { 
  128.         super(); 
  129.         this.superior = boss; 
  130.         this.title = "Secretary";
  131.     } 
  132.     //METHOD 
  133.     public boolean canHandle(Letter kvetch) 
  134.     { 
  135.         //The Secretary only handles accusations. 
  136.         if(kvetch.getSubject().equalsIgnoreCase("Accusation")) return true
  137.         else return false;
  138.     }
  139.   
  140. //The concrete extender of HANDLER, the LACKEY class: 
  141. public class Lackie extends Handler 
  142.     //CONSTRUCTOR 
  143.     public Lackie(Handler boss) 
  144.     { 
  145.         super(); 
  146.         this.superior = boss; 
  147.         this.title = "Lackie";
  148.     } 
  149.     //METHOD 
  150.     public boolean canHandle(Letter kvetch) 
  151.     { 
  152.         //The Lackey only handles inquiries. 
  153.         if(kvetch.getSubject().equalsIgnoreCase("Inquiry")) return true
  154.         else return false;
  155.     }
  156. public class MailClerk extends Handler 
  157.     //CONSTRCUTOR 
  158.     public MailClerk(Handler boss) 
  159.     { 
  160.         super(); 
  161.         this.superior = boss; 
  162.         this.title = "Mail Clerk";
  163.     } 
  164.     //METHOD 
  165.     public boolean canHandle(Letter kvetch) 
  166.     { 
  167.         //The Mail Clerk only handles criticisms. 
  168.         if(kvetch.getSubject().equalsIgnoreCase("Criticism")) return true
  169.         else return false;
  170.     }
  171. public class ChainDemo2 {
  172.     public static void main(String[] args) {
  173.         //Instantiate the chain IN THIS ORDER!!! 
  174.         //Otherwise, they don't have bosses to use in their constructors. 
  175.         Governer taft = new Governer(); 
  176.         Secretary marcia = new Secretary(taft); 
  177.         Lackie joe = new Lackie(marcia); 
  178.         MailClerk annie = new MailClerk(joe);  
  179.     }
  180. }

 When to use 


This pattern deals with the relationship between a set of objects and a request. You apply this pattern when more than one object can handle a request. The first object in the chain gets the request and either resolves it or moves it along to the next object in the chain, until one can handle it. The original requesting object doesn't know which object will handle its request. The object that ultimately handles the request it said to be and implicit receiver. Restaurants are set up this way; a customer typically doesn't send a request directly to a chef and isn't usually acquainted with the chef the request is going to do. Instead, the customer gives and order to a server, the server gets it to the chef, who might fulfils the order or pass it along to an assistant chef. 
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有