» The Chain of Responsibility pattern is concerned with passing requests for action to appropriate objects; » The Chain of Responsibility pattern works with a list of handler objects that have limitations on the nature of the requests they can deal with; if an object cannot handle a request, it passes it on to the next object in the chain; at the end of the chain, there can be either default or exceptional behaviour; to handle a request, the handler must have the appropriate resources, knowledge, and permissions.
Use When:
» You have more than one handler for a request; » You have reasons why a handler should pass a request on to another one in the chain; » You have a set of handlers that varies dynamically.
//Define the Pattern Interface and List of Handler Objects
//Define the IApprover interface to be used as the "blueprint" for the main application classes. //This interface defines the ProcessRequest method. This method represents the core functionality within the Chain Of Responsibility pattern, //and will either perform the required processing or decide to pass the request onto the next object in the chain. public interfaceIApprover { void ProcessRequest(Purchase purchase); }
//The Director class (object) represents the first element in the chain. //This class inherits from the IApprover interface, and therefore implements the ProcessRequest method. //The class defines a private variable (m_successor) of type IApprover, which will store the object that represents the next element in the chain. //In this case, the object would be of type VicePresident. //The Director class (object) will automatically approve any purchase orders that have an amount less than 10000.00. //If the value exceeds 10000.00, the request will be passed onto the VicePresident class (object) for approval. public classDirector : IApprover { privateIApprover _mSuccessor;
public Director(IApprover successor) { _mSuccessor = successor; }
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 10000.0) { Console.WriteLine("{0} approved request {1}", GetType().Name, purchase.PurchaseId); } else if (_mSuccessor != null) { _mSuccessor.ProcessRequest(purchase); } } }
//The VicePresident class (object) represents the second element in the chain. //This class inherits from the IApprover interface, and thefore implements the ProcessRequest method. //The class defines a private variable (m_successor) of type IApprover, which will store the object that represents the next element in the chain. //In this case, the object would be of type President. //The VicePresident class (object) will automatically approve any purchase orders that have an amount less than 25000.00. //If the value exceeds 25000.00, the request will be passed onto the President class (object) for approval. public classVicePresident : IApprover { privateIApprover _mSuccessor;
public VicePresident(IApprover successor) { _mSuccessor = successor; }
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 25000.0) { Console.WriteLine("{0} approved request {1}", GetType().Name, purchase.PurchaseId); } else if (_mSuccessor != null) { _mSuccessor.ProcessRequest(purchase); } } }
//The President class (object) represents the final element in the chain. //This class inherits from the IApprover interface, and thefore implements the ProcessRequest method. //The President class (object) will only approve purchase orders that have an amount less than 100000.00. public classPresident : IApprover { public President() {
}
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 100000.0) { Console.WriteLine("{0} approved request {1}", GetType().Name, purchase.PurchaseId); } else) { Console.WriteLine("{0} requires an executive meeting!", purchase.PurchaseId); } } }
//Define the Pattern Interface and List of Handler Objects
//Define the IApprover interface to be used as the "blueprint" for the main application classes. //This interface defines the ProcessRequest method. This method represents the core functionality within the Chain Of Responsibility pattern, //and will either perform the required processing or decide to pass the request onto the next object in the chain. public interfaceIApprover { void ProcessRequest(Purchase purchase); }
//The Director class (object) represents the first element in the chain. //This class inherits from the IApprover interface, and therefore implements the ProcessRequest method. //The class defines a private variable (m_successor) of type IApprover, which will store the object that represents the next element in the chain. //In this case, the object would be of type VicePresident. //The Director class (object) will automatically approve any purchase orders that have an amount less than 10000.00. //If the value exceeds 10000.00, the request will be passed onto the VicePresident class (object) for approval. public classDirector : IApprover { privateIApprover _mSuccessor;
public Director(IApprover successor) { _mSuccessor = successor; }
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 10000.0) { Console.WriteLine("{0} approved request {1}", GetType().Name, purchase.PurchaseId); } else if (_mSuccessor != null) { _mSuccessor.ProcessRequest(purchase); } } }
//The VicePresident class (object) represents the second element in the chain. //This class inherits from the IApprover interface, and thefore implements the ProcessRequest method. //The class defines a private variable (m_successor) of type IApprover, which will store the object that represents the next element in the chain. //In this case, the object would be of type President. //The VicePresident class (object) will automatically approve any purchase orders that have an amount less than 25000.00. //If the value exceeds 25000.00, the request will be passed onto the President class (object) for approval. public classVicePresident : IApprover { privateIApprover _mSuccessor;
public VicePresident(IApprover successor) { _mSuccessor = successor; }
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 25000.0) { Console.WriteLine("{0} approved request {1}", GetType().Name, purchase.PurchaseId); } else if (_mSuccessor != null) { _mSuccessor.ProcessRequest(purchase); } } }
//The President class (object) represents the final element in the chain. //This class inherits from the IApprover interface, and thefore implements the ProcessRequest method. //The President class (object) will only approve purchase orders that have an amount less than 100000.00. public classPresident : IApprover { public President() {
}
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 100000.0) { Console.WriteLine("{0} approved request {1}", GetType().Name, purchase.PurchaseId); } else) { Console.WriteLine("{0} requires an executive meeting!", purchase.PurchaseId); } } }