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?
If I understand you correctly...what you could do is remove the
SetupHandler
method and introduce anOnElevateQuery
event were yourCriticalHelpDesk
object could handle theFrontLineHelpDesk.OnElevateQuery
event and yourLegalHelpDesk
object could handle theCriticalHelpDesk.OnElevateQuery
event. TheOnElevateQuery
event could pass the customer in the event args.Example
Usage
However, as the query type is based on the enum
ComplaintType
have you considered using perhaps aHelpDeskFactory
which could return a generic interface e.g.IHelpDesk
. Sounds like you could also use the Strategy Pattern for this particular example.This is very similar to above answers, but more streamlined. :)
Customer having a complaintType looks like a misplaced attribute. I assume you mean a Complaint has a Type.
I may be wrong in which case you can point what behavior is missing This just looks like an event to me. Each event handler would be called in order of subscription. Each handler is free to ignore the notification based on the complaint. The next handler is called as long as the Handled property of the eventArgs is false and there are pending subscribers.