A day with .Net

My day to day experince in .net

Chain Of Responsibility Design Pattern in C#

Posted by vivekcek on September 10, 2012

read about it here http://www.dofactory.com/Patterns/PatternChain.aspx#_self2

Here i am giving a simple example from my organization.
In our organization if i need a leave i have to submit a leave form to someone just above me. The person just above me is my mentor.If i need a leave for 1 day he can immediately approve my leave. If i need more than 1 day he will contact Project manger he is just above the mentor. The project manger can approve leave upto 2 days.If i need leave more than 2 days, the project manger consult HR Manger.HR Manger can approve leave up-to 4 days.If i request more than 4 days leave my request will be forwarded to CEO. CEO will approve leave’s up-to 5 days.If i need more than 5 day’s of leave CEO will call me for a meeting.

In all the above case i just need to submit leave application to my mentor. The approval process propagate automatically and i will get some response.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChainOfResponsibility
{
    class Program
    {
        static void Main(string[] args)
        {
            int NumberOfDays;
            var vivek = new SoftwareEngineer();
            var myMentor = new Mentor();
            var myPM = new ProjectManger();
            var myHR = new HRManger();
            var myCEO = new CEO();

            vivek.SetNextLevelEmployee(myMentor);
            myMentor.SetNextLevelEmployee(myPM);
            myPM.SetNextLevelEmployee(myHR);
            myHR.SetNextLevelEmployee(myCEO);

            while (true)
            {
                Console.WriteLine("Enter Number Of Day's Of Leave");
                NumberOfDays = int.Parse(Console.ReadLine());
                vivek.ProcessLeave(NumberOfDays);
            }

        }
    }
    public abstract class Employee
    {
        protected Employee NextLevelEmployee;
        public abstract void ProcessLeave(int numberOfDays);
        public void SetNextLevelEmployee(Employee emp)
        {
            NextLevelEmployee = emp;
        }
    }
    public class SoftwareEngineer : Employee
    {

        public override void ProcessLeave(int numberOfDays)
        {
            if (NextLevelEmployee != null)
            {
                NextLevelEmployee.ProcessLeave(numberOfDays);
            }
        }
    }
    public class Mentor : Employee
    {

        public override void ProcessLeave(int numberOfDays)
        {
            if(numberOfDays<2)
            {
                Console.WriteLine("Leave approved by Mentor");
            }
            else if (NextLevelEmployee != null)
            {
                NextLevelEmployee.ProcessLeave(numberOfDays);
            }
        }
    }
    public class ProjectManger : Employee
    {

        public override void ProcessLeave(int numberOfDays)
        {
            if (numberOfDays <=3)
            {
                Console.WriteLine("Leave approved by Project Manger");
            }
            else if (NextLevelEmployee != null)
            {
                NextLevelEmployee.ProcessLeave(numberOfDays);
            }
        }
    }
    public class HRManger : Employee
    {

        public override void ProcessLeave(int numberOfDays)
        {
            if (numberOfDays <=4)
            {
                Console.WriteLine("Leave approved by HRMnager");
            }
            else if (NextLevelEmployee != null)
            {
                NextLevelEmployee.ProcessLeave(numberOfDays);
            }
        }
    }
    public class CEO : Employee
    {

        public override void ProcessLeave(int numberOfDays)
        {
            if (numberOfDays <= 5)
            {
                Console.WriteLine("Leave approved by CEO");
            }
            else 
            {
                Console.WriteLine("Let us have a meeting with CEO");
            }
        }
    }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s