Chain Of Responsibility Pattern Code Review
Define the Pattern Interface and List of Handler Objects
Code Download
- Download Description:chain of responsibility pattern download
- .NET Framework:3.5
- .NET Language:C#
- Date Published:2009-07-01
- Download Size:9 KB
Code Review
- Chain of Responsibility Pattern Code Review: Define the Pattern Interface and List of Handler Objects
- Chain of Responsibility Pattern Code Review: Use the Chain of Responsibility Pattern
Code Walkthrough
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.
The Director class (object) represents the first element in the chain.
public Director(IApprover successor) { this.m_successor = successor; }
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 10000.0) { Console.WriteLine("{0} approved request {1}", this.GetType().Name, purchase.PurchaseID); } else if (this.m_successor != null) { this.m_successor.ProcessRequest(purchase); } } }
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.
The VicePresident class (object) represents the second element in the chain.
public VicePresident(IApprover successor) { this.m_successor = successor; }
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 25000.0) { Console.WriteLine("{0} approved request {1}", this.GetType().Name, purchase.PurchaseID); } else if (this.m_successor != null) { this.m_successor.ProcessRequest(purchase); } } }
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.
The President class (object) represents the final element in the chain.
public void ProcessRequest(Purchase purchase) { if (purchase.Amount < 100000.0) { Console.WriteLine("{0} approved request {1}", this.GetType().Name, purchase.PurchaseID); } else { Console.WriteLine("{0} requires an executive meeting!", purchase.PurchaseID); } } }
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.
Printer Friendly Version
Add to Favourites
DotNet Kicks
Digg
del.icio.us
Live Favourites
ma.gnolia
reddit
Slashdot
Technorati
Yahoo!