The design pattern Chain of Responsibility

  Abstract: The design pattern Chain of Responsibility 

  Chain of Responsibility definition 
  Chain of Responsibility (CoR) is using a series of categories (classes) to try and deal with a request request, and between these categories is a loose coupling, is the only common ground between them transfer request. Other words, a request to Type A deal with, if not dealt with, transmitted to the Class B treatment, if not dealt with, on the transfer to category C treatment, it is so like a chain (chain) as pass on. 

  How to use it? 
  While this section is how to use CoR, but also demonstrated what is CoR. 

  One Handler Interface: 

  (Public interface Handler 
  Public void handleRequest (); 
  ) 

  This is a case dealing with the request, if there are multiple request, such as asking for help or request formatted print requests: 

  The first to think of solutions: increase in the number of requests interface: 
  (Public interface Handler 
  Public void handleHelp (); 
  Public void handlePrint (); 
  Public void handleFormat (); 

  ) 

  Implementation is a specific interface Handler code: 
  Public class ConcreteHandler implements Handler ( 
  Private Handler successor; 

  Public ConcreteHandler (Handler successor) ( 
  This.successor = successor; 
  ) 

  Public void handleHelp () ( 
  / / Help specific handling of the request code … 
  ) 

  Public void handlePrint () ( 
  / / If it is to deal with the Print to print 
  Successor.handlePrint (); 
  ) 
  Public void handleFormat () ( 
  / / If it is to deal with the format to the Format 
  Successor.handleFormat (); 
  ) 

  ) 
  There are three concrete realization of this category, the above is handled help, and deal with handling Print Format This is probably the most commonly used programming ideas. 

  Although the ideas simple and clear, but there is a question of expansion, if we need to add a request request types, and the need to amend the interface to achieve each one. 

  The second programme: each request will be turned into an interface, we have the following code: 

  (Public interface HelpHandler 
  Public void handleHelp (); 
  ) 

  (Public interface PrintHandler 
  Public void handlePrint (); 
  ) 

  (Public interface FormatHandler 
  Public void handleFormat (); 
  ) 

  Public class ConcreteHandler 
  Implements HelpHandler, PrintHandler, FormatHandlet ( 
  Private HelpHandler helpSuccessor; 
  Private PrintHandler printSuccessor; 
  Private FormatHandler formatSuccessor; 

  Public ConcreteHandler (HelpHandler helpSuccessor, PrintHandler printSuccessor, FormatHandler formatSuccessor) 
  ( 
  This.helpSuccessor = helpSuccessor; 
  This.printSuccessor = printSuccessor; 
  This.formatSuccessor = formatSuccessor; 
  ) 

  Public void handleHelp () ( 
…….
  ) 

  Public void handlePrint () = (this.printSuccessor printSuccessor;) 

  Public void handleFormat () = (this.formatSuccessor formatSuccessor;) 

  ) 

  This approach in the new request at the request of circumstances, the only saving changes in the interface, the interface ConcreteHandler also needs to be revised.    And the code is obviously something beautiful. 

  Solution 3: Handler Interface uses only one parameter: 
  (Public interface Handler 
  Public void handleRequest (String request); 
  ) 
  Then Handler codes are as follows: 
  Public class ConcreteHandler implements Handler ( 
  Private Handler successor; 

  Public ConcreteHandler (Handler successor) ( 
  This.successor = successor; 
  ) 

  Public void handleRequest (String request) ( 
  If (request.equals ( "Help")) ( 
  / / Here is dealing with the specific code Help else) 
  / / Successor.handle transfer to a (request); 

  ) 
  ) 

  ) 


  Here is the first assumption String type of request, if not how do?    Of course, we can create a special category Request 


  Finally solution: Interface Handler code as follows: 
  (Public interface Handler 
  Public void handleRequest (Request request); 
  ) 
  Request class definition: 
  (Public class Request 
  Private String type; 

  Public Request (String type) (this.type = type;) 

  Public String getType () (return type;) 

  Public void execute () ( 
  / / Request specific acts of genuine code) 
  ) 
  Then Handler codes are as follows: 
  Public class ConcreteHandler implements Handler ( 
  Private Handler successor; 

  Public ConcreteHandler (Handler successor) ( 
  This.successor = successor; 
  ) 

  Public void handleRequest (Request request) ( 
  If (request instanceof HelpRequest) ( 
  / / Here is dealing with the specific code Help) else if (request instanceof PrintRequst) ( 
  Request.execute (); 
  ) Else 
  / / Successor.handle transfer to a (request); 

  ) 
  ) 

  ) 

  This solution is CoR, in a chain, there are corresponding responsibilities category, called Chain of Responsibility. 

  CoR advantages: 
  From the outside world because of their inability to foresee the request is a type, encountered each category if it can not handle the request as long as we can give up.    No doubt, this reduces the coupling between categories. 

  Deficiency is inefficient, because the completion of a request may have to traverse to be completed before the end, of course, can also use the concept of tree optimization.    In Java AWT1.0, in regard to the matters dealt with the mouse button is the use of CoR to Java.1.1 later, on the use of substitute CoR Observer 

  Expansion of the poor, because the CoR, there must be a unified interface Handler. Limitations on here. 



  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply