Chain Of Responsibility Pattern in C#

                                       

                         Chain Of Responsibility Pattern in C#



Definition:


                    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.

Details:

                Client: Initiates a request to a ConcreteHandler object on the chain.

                 Handler: defines an interface for handling the requests. 

                 ConcreteHandler:

        • handles requests its responsible for. 
        • can access its successor. 
        • if ConcreteHandler can handle the request, it does so. otherwise it forwards the request to its successor.                          

 Implementation with Example:

Say, you are working for a company and you report to a Manager, Manager reports to Director and Director to CEO. 

You are a new employee and would like to request for the following resources to get you started at work:
                  
  • Laptop\Desktop.   - This needs only approval only from Manager.
  • Access to Production Machines if you are a developer.  - This would need an approval from Director. 

 Step 1: We define a Handler- which is an interface for handling requests. In our case, we call it Authority

  abstract class Authority
 {
    //We defined the Successor here. 
    protected Authority _approver; 
    public void SetApprover(Authority approver)
    {
       this._approver= approver;
    }

     // We define the method which will approve the process. 
    public abstract void WorkOnRequests(Request request);
 }


Step 2: We define the Hierarchy in the organization such as Manager, Director and CEO as seperate Concrete Handlers which inherit Authority Class(Handler). 
    public class Manager : Authority
    {
        public override void WorkOnRequests(Request request)
        {
            if (request.Item == "Laptop" || request.Item == "Desktop")
            {
                Console.WriteLine("{0} has been approved by {1}",
                request.Item, this.GetType().Name);
            }
            else if (_approver != null)
            {
                _approver.WorkOnRequests(request);
            }
        }
    }
    public class Director : Authority
    {
        public override void WorkOnRequests(Request request)
        {
            if (request.Item == "Production Access")
            {
                Console.WriteLine("{0} has been approved by {1}",
                request.Item, this.GetType().Name);
            }
            else if (_approver != null)
            {
                _approver.WorkOnRequests(request);
            }
        }
    }

   class Request
  {
    private string _item;
 
    // Constructor

    public Request(string item)
    {
      this._item = item;
    }
 
    // Gets or sets Item.

    public string Item
    {
      get { return _item; }
      set { _item = value; }
    }
 
  }

Step 3: Add the Client Class. Call from the client class.

   public class MainApp 
   {
    static void Main()
    {
      //Chain of Responsibility

      Authority sree = new Directors();
      Authority sam = new Manager();

      sree.SetApprover(sam);

            // Generate and process purchase requests

      Request r = new Request("Laptop");
      sree.WorkOnRequests(r);

      r = new Request("Production Access");
      sree.WorkOnRequests(r);

      // Wait for user
      Console.ReadKey();
    }
   }
   
Out of this Code would be:
Laptop has been approved by Manager
Production Access has been approved by Director

                     

                    

Comments

Popular posts from this blog

Knowing too much is bad(in the world of OOP)

Reverse a LinkedList Algorithm

DateTime.Now in C#