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.
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.
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.
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.
The Program class creates an instance of the President, VicePresident and Director classes.
On creation of the VicePresident object, the President object is passed into the constructor of the VicePresident object. In the same way, the VicePresident object is passed into the constructor of the Director class. The object passed into the respective constructor represents the next object in the chain, from the point-of-view of the object being created.