views:

297

answers:

3

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?

+3  A: 

If I understand you correctly...what you could do is remove the SetupHandler method and introduce an OnElevateQuery event were your CriticalHelpDesk object could handle the FrontLineHelpDesk.OnElevateQuery event and your LegalHelpDesk object could handle the CriticalHelpDesk.OnElevateQuery event. The OnElevateQuery event could pass the customer in the event args.

Example

public abstract class CustomerServiceDesk
{
    public delegate void ElevateQueryEventHandler(Customer c);
    public event ElevateQueryEventHandler OnElevateQuery;
    public abstract void ServeCustomer(Customer c);
}

public class FrontLineServiceDesk : CustomerServiceDesk
{
    public override void ServeCustomer(Customer c)
    {
        switch (c.ComplaintType)
        {
            case ComplaintType.General:
                Console.WriteLine(c.Name + " Complaints are registered; will be served soon by FrontLine Help Desk");
                break;
            default:
                OnElevateQuery(c);
        }
    }
}

public class CriticalIssueServiceDesk : CustomerServiceDesk
{
    public override void ServeCustomer(Customer c)
    {
        switch (c.ComplaintType)
        {
            case ComplaintType.Critical:
                Console.WriteLine(c.Name + " Complaints are registered; will be served soon by Critical Help Desk");
                break;
            case ComplaintType.Legal:
                OnElevateQuery(c);
                break;
            default:
                Console.WriteLine("Unable to find appropriate help desk for your complaint.");
                break;
        }
    }
}

public class LegalIssueServiceDesk : CustomerServiceDesk
{
    public override void ServeCustomer(Customer c)
    {
        if (c.CompliantType == CompliantType.Legal)
        {
            Console.WriteLine(c.Name + " Complaints are registered; will be served soon by Legal Help Desk");
        }
        else
        {
            // you could even hook up the FrontLine.ServeCustomer event 
            // to the OnElevateQuery event of this one so it takes the 
            // query back to the start of the chain (if it accidently ended up here).
            Console.WriteLine("Wrong department");
        }
    }
}

Usage

CustomerServiceDesk _frontLine = new FrontLineServiceDesk();
CustomerServiceDesk _criticalLine = new CriticalLineServiceDesk();
CustomerServiceDesk _legalLine = new LegalLineServiceDesk();
// hook up events
_frontLine.OnElevateQuery += _critialLine.ServeCustomer;
_criticalLine.OnElevateQuery += _legalLine.ServeCustomer;

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;

_frontLine.ServeCustomer(_customer1);
_frontLine.ServeCustomer(_customer2);
_frontLine.ServeCustomer(_customer3);

However, as the query type is based on the enum ComplaintType have you considered using perhaps a HelpDeskFactory which could return a generic interface e.g. IHelpDesk. Sounds like you could also use the Strategy Pattern for this particular example.

James
Excellent Explanation,Thank you for suggesting HelpDeskFactory.
@nettguy: no problem :)
James
+1  A: 

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.

class ComplaintSource
{
  public delegate void ComplaintHandler(Complaint complaint, HandledEventArgs evtArgs);
  public event ComplaintHandler NewComplaint;

  // code that raises the NewComplaint event as appropriate.
   public void DoStuffThatRaisesTheEvent()
    {
        var evtArgs = new HandledEventArgs();
        var theComplaint = new Complaint();
        if (null == this.NewComplaint)
            return;

        Delegate[] list = NewComplaint.GetInvocationList();
        foreach (Delegate del in list)
        {
            if (evtArgs.Handled)
                break;
            ComplaintHandler handler = (ComplaintHandler)del;
            handler(theComplaint, evtArgs);
        }
    }
}

class FrontLineServiceDesk
{
  FrontLineServiceDesk(ComplaintSource source)
  { source.NewComplaint += HandleGeneralComplaint; }
  void HandleGeneralComplaint(Complaint complaint, HandledEventArgs evtArgs) { ... 
    // set evtArgs.Handled = true if you've handled the complaint
    // this will stop the chain
  }
}

class CriticalIssueServiceDesk
{
  CriticalIssueServiceDesk(ComplaintSource source)
  { source.NewComplaint += HandleGeneralComplaint; }
  void HandleCriticalComplaint(Complaint complaint, HandledEventArgs evtArgs) { ... }
}

// finally set the ball in motion

var source = new CompaintSource();
var frontLineDesk = new FrontLineServiceDesk(source);
var criticalIssueDesk = new CriticalIssueServiceDesk(source);

source.DoStuffThatRaisesTheEvent();
Gishu
He's trying to understand chain-of-responsibility, not write the best code to do this job.
Steven Mackenzie
@Gishu Thank you very much
A: 

This is very similar to above answers, but more streamlined. :)

public abstract class CustomerServiceDesk
{
    protected CustomerServiceDesk()
    {
        ServeCustomers = doServeCustomers;
    }

    protected CustomerServiceDesk m_ServiceDesk = null;
    protected abstract void doServeCustomers(Customer _customer);

    public delegate void ServeCustomersDelegate(Customer _customer);
    public ServeCustomersDelegate ServeCustomers = null;
}

public class LegalissueServiceDesk : CustomerServiceDesk
{
    public LegalissueServiceDesk()
    {
    }

    protected override void doServeCustomers(Customer _customer)
    {
        if (_customer.ComplaintType == ComplaintType.Legal)
        {
            Console.WriteLine(_customer.Name + " - Complaints are registered  ; will be served soon by legal help desk.\n");
        }
    }
}

public class CriticalIssueServiceDesk : CustomerServiceDesk
{
    public CriticalIssueServiceDesk()
    {
        m_ServiceDesk = new LegalissueServiceDesk();
        ServeCustomers += m_ServiceDesk.ServeCustomers;
    }

    protected override void doServeCustomers(Customer _customer)
    {
        if (_customer.ComplaintType == ComplaintType.Critical)
        {
            Console.WriteLine(_customer.Name + " - Complaints are registered  ; will be served soon by Critical Help Desk.\n");
        }
    }
}

public class FrontLineServiceDesk : CustomerServiceDesk
{
    public FrontLineServiceDesk()
    {
        m_ServiceDesk = new CriticalIssueServiceDesk();
        ServeCustomers += m_ServiceDesk.ServeCustomers;
    }

    protected override void doServeCustomers(Customer _customer)
    {
        if (_customer.ComplaintType == ComplaintType.General)
        {
            Console.WriteLine(_customer.Name + " - Complaints are registered  ; will be served soon by FrontLine Help Desk.\n");
        }
    }
}

public class Customer
{
    public string Name;
    public ComplaintType ComplaintType;
}

public enum ComplaintType
{
    General,
    Critical,
    Legal
}

class Program
{
    static void Main(string[] args)
    {
        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;

        FrontLineServiceDesk _frontLineDesk = new FrontLineServiceDesk();

        _frontLineDesk.ServeCustomers(_customer1);
        _frontLineDesk.ServeCustomers(_customer2);
        _frontLineDesk.ServeCustomers(_customer3);

        Console.In.ReadLine();
    }
}
Nayan
Love the fact that the streamlined version is longer than the rest. On the no-kidding side, this version has kind of hardcoded the stages. e.g. to add a new intermediate step in the chain, you'd have to modify an existing class - more importantly each stage has knowledge about the next stage (and contains/creates it in this case). I'd loosen the coupling here..
Gishu
Yes, I agree. Its more of design change. It may seem logical to put it this way.. but needs may differ. I just presented my idea. Truly, it depends on need of the design. Your example do the job well enough to demonstrate the use of delegates. BTW, the code may seem longer than yours because you omitted most of it =D
Nayan