Difference between revisions of "Chain of Responsibility"

From CDOT Wiki
Jump to: navigation, search
Line 1: Line 1:
 +
----
 +
== Chain of Responsibility (Behavioral Pattern) ==
 +
 
* The '''Chain of Responsibility''' pattern uses a chain of objects to handle a request, which is typically an event. Objects in the chain forward the request along the chain until one of the objects handles the event. Processing stops after an event is handled.
 
* The '''Chain of Responsibility''' pattern uses a chain of objects to handle a request, which is typically an event. Objects in the chain forward the request along the chain until one of the objects handles the event. Processing stops after an event is handled.
  
 
[[Image:CoR1.jpg]]
 
[[Image:CoR1.jpg]]
 
 
----
 
----
 
== Purpose ==
 
== Purpose ==
  
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.
+
Is to 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.
  
  
Line 14: Line 16:
  
 
[[Image:CoR_UML.jpg]]
 
[[Image:CoR_UML.jpg]]
 +
  
 
The role of every class:
 
The role of every class:
 +
 
*'''Handler''' - defines an interface for handling requests
 
*'''Handler''' - defines an interface for handling requests
 
*'''RequestHandler:'''
 
*'''RequestHandler:'''
Line 24: Line 28:
 
----
 
----
 
==Applicability==
 
==Applicability==
 +
 
Here are a few situations when using the Chain of Responsibility is more effective:
 
Here are a few situations when using the Chain of Responsibility is more effective:
 +
 
* More than one object can handle a request
 
* More than one object can handle a request
  
Line 34: Line 40:
  
 
* The group of objects that may handle the request must be specified in a dynamic way
 
* The group of objects that may handle the request must be specified in a dynamic way
 +
 
----
 
----
  

Revision as of 16:13, 2 April 2007


Chain of Responsibility (Behavioral Pattern)

  • The Chain of Responsibility pattern uses a chain of objects to handle a request, which is typically an event. Objects in the chain forward the request along the chain until one of the objects handles the event. Processing stops after an event is handled.

CoR1.jpg


Purpose

  • Is to 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.



UML Diagram

CoR UML.jpg


The role of every class:

  • Handler - defines an interface for handling requests
  • RequestHandler:
    • handles the requests it is responsible for
    • If it can handle the request it does so, otherwise it sends the request to its successor
  • Client - sends commands to the first object in the chain that may handle the command

Applicability

Here are a few situations when using the Chain of Responsibility is more effective:

  • More than one object can handle a request
  • The handler is not known in advance
  • The handler should be determined automatically
  • It’s wished that the request is addressed to a group of objects without explicitly specifying its receiver
  • The group of objects that may handle the request must be specified in a dynamic way


External Links

  • External Links


--Djeyarat 15:49, 2 April 2007 (EDT)