Thursday, 7 September 2017

chain of responsibility Design Pattern

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.


Suppose there is customer who has applied for loan amount then it will go for approval as
per the loan amount increase

0-1000 -Cashier
1000-5000-Manager
>5000 -HOD


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSApp.Behavioural.COR;

namespace DSApp
{
    class Program
    {
        static void Main(string[] args)
        {
            goto CORLabel;

        CORLabel: {
            Console.WriteLine("Enter Loan Amount");
            int LoanAmount =Convert.ToInt32( Console.ReadLine());
            Cashier cashier = new Cashier();
            Manager manager = new Manager();
            HOD hod = new HOD();

            cashier.sucessor = manager;
            manager.sucessor = hod;

            cashier.Approve(LoanAmount);
            

        }
        }
    }
}

-------------------------------------------


namespace DSApp.Behavioural.COR
{
   public interface IOperation
    {
        void Approve(int amount);

    }
}

---------------------------------------------


namespace DSApp.Behavioural.COR
{
   public  class LoanEntity
    {
       public int loanAmnt;

    }
}
----------------------------------------

 public  class Cashier:IOperation
    {
       public IOperation sucessor;
       
       public void Approve(int amount)
       {
           if (amount <= 1000 && amount > 0)
           {
               Console.WriteLine("Loan approved by Cashier");
           }
           else
           {
               sucessor.Approve(amount);
           }
       }
    }

-------------------------------------------


public class HOD:IOperation
    {
      public  IOperation sucessor
        {
            get;
            set;
        }
        public void Approve(int amount)
        {
            if (amount >5000)
            {
                Console.WriteLine("Loan approved by HOD");
            }
            else
            {
                sucessor.Approve(amount);
            }
            
            
        }
    }


No comments:

Post a Comment