Chain Of Responsibility Pattern Code Review

Define the Pattern Interface and List of Handler Objects

Code Download

Code Review

Code Walkthrough

Define the IApprover interface to be used as the "blueprint" for the main application classes.

interface IApprover { void ProcessRequest(Purchase purchase); }

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.

class Director : IApprover { IApprover m_successor;
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.

class VicePresident : IApprover { IApprover m_successor;
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.

class President : IApprover { public President() { }
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.