C# Chain-of-responsibility with delegates
- by nettguy
For my understanding purpose i have implemented Chain-Of-Responsibility pattern.
//Abstract Base Type
public abstract class CustomerServiceDesk
{
protected CustomerServiceDesk _nextHandler;
public abstract void ServeCustomers(Customer _customer);
public void SetupHadler(CustomerServiceDesk _nextHandler)
{
this._nextHandler = _nextHandler;
}
}
public class FrontLineServiceDesk:CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.General)
{
Console.WriteLine(_customer.Name + " Complaints are registered ;
will be served soon by FrontLine Help Desk..");
}
else
{
Console.WriteLine(_customer.Name + "
is redirected to Critical Help Desk");
_nextHandler.ServeCustomers(_customer);
}
}
}
public class CriticalIssueServiceDesk:CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.Critical)
{
Console.WriteLine(_customer.Name +
"Complaints are registered ; will be served soon
by Critical Help Desk");
}
else if (_customer.ComplaintType == ComplaintType.Legal)
{
Console.WriteLine(_customer.Name +
"is redirected to Legal Help Desk");
_nextHandler.ServeCustomers(_customer);
}
}
}
public class LegalissueServiceDesk :CustomerServiceDesk
{
public override void ServeCustomers(Customer _customer)
{
if (_customer.ComplaintType == ComplaintType.Legal)
{
Console.WriteLine(_customer.Name +
"Complaints are registered ;
will be served soon by legal help desk");
}
}
}
public class Customer
{
public string Name { get; set; }
public ComplaintType ComplaintType { get; set; }
}
public enum ComplaintType
{
General,
Critical,
Legal
}
void Main()
{
CustomerServiceDesk _frontLineDesk = new FrontLineServiceDesk();
CustomerServiceDesk _criticalSupportDesk = new CriticalIssueServiceDesk();
CustomerServiceDesk _legalSupportDesk = new LegalissueServiceDesk();
_frontLineDesk.SetupHadler(_criticalSupportDesk);
_criticalSupportDesk.SetupHadler(_legalSupportDesk);
Customer _customer1 = new Customer();
_customer1.Name = "Microsoft";
_customer1.ComplaintType = ComplaintType.General;
Customer _customer2 = new Customer();
_customer2.Name = "SunSystems";
_customer2.ComplaintType = ComplaintType.Critical;
Customer _customer3 = new Customer();
_customer3.Name = "HP";
_customer3.ComplaintType = ComplaintType.Legal;
_frontLineDesk.ServeCustomers(_customer1);
_frontLineDesk.ServeCustomers(_customer2);
_frontLineDesk.ServeCustomers(_customer3);
}
Question
Without breaking the chain-of-responsibility ,how can i apply delegates and events to rewrite the code?