JAVA学习网首页 把生活查询网加入收藏 把JAVA学习网设为首页 联系方式
    Hi,JAVA学习
JAVA基础 设计模式 数据库 JavaBeans J2EE JavaDelphi 用户界面 综合文章  
[Behavioral Patterns] Chain of Responsibility
时间:22/04/2007
作者:网络
来源:网络
小提示点这里把文章加入您的收藏夹,方便下次查看
设置文章字体大小:[ ]

 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. 
上一篇:pet store 的设计目标

下一篇:Take Command of Your Software (1)

  • Javamail操作指南(二)
  • 告别System.out.print()—J2SDK1.4新增Java日志框架(一)
  • 用SQLJ开发数据库
  • 使用org.apache.commons.beanutils个人感受
  • 一个Jsp初学者的学习过程(三)
  • JDK Observer设计模式之研究
  • 一个判断session是否过期的小技巧
  • 《Effective Java》学习笔记(3)
  • 我对构架MVC的理解
  • 使用多线程技术让你的Swing及时响应各类事件
  • Portal开源实现-Liferay的Portlet Session处理(1)
  • JAVA的学习体会
  • 无进度条刷新获得服务器端数据并相应处理
  • 屏蔽鼠标右键,F1帮助和常用快捷键
  • Adapter模式在J2SE事件处理中的应用
  • 对synchronized(this)的一些理解
  • JAVA专业术语集
  • java程序如何穿透带有密码验证的代理
  • 返回】 【顶部】 【关闭
    Copyright © 2005-2010 www.594k.com All Rights Reserved.
    版权所有:JAVA学习网 备案序号:皖ICP备06004238号