Dec 16 2009

Overview of the Chain of Responsibility Design Pattern

Category: Design Patternryancmartin1976 @ 17:41

First Strategy:

Second Strategy:

Chain of Responsibility Design Pattern in C#

This pattern decouples events from the sender object to the receiver object. The sender and receiver are linked together and hieratical, hence the name chain of responsibility.

What does that mean?

We have three objects that are linked and communicate with one another. One object passes an event to the next object until one of those objects can satisfy the event being passed.

Dog, Bird, Fish all derive from Animal but only one of them can run but all of them can move. We would have a move method which could have a run event which the Dog object could execute perfectly.

public interface IProduct

{

    void AddLicense(UserInfo userInfo, string parentSerialNumber);

    int AddLicenseStatus(UserInfo userInfo);

    void AddLeadIntoCRM(UserInfo userInfo, string parentSerial);

}

 

public class PremiumProduct : IProduct

{

    private ILicense _license;

    private ILogging _logging;

 

    public PremiumProduct()

    {

        _license = new ParentLicense();

        _logging = new ParentLogging();

    }

 

    public virtual void AddLicense(UserInfo userInfo, string parentSerialNumber)

    {

        _license.AddLicense(this, userInfo, parentSerialNumber);

    }

 

    public virtual int AddLicenseStatus(UserInfo userInfo)

    {

        return _license.AddLicenseStatus(this, userInfo);

    }

 

    public virtual void AddLeadIntoCRM(UserInfo userInfo, string parentSerial)

    {

        _logging.AddLeadIntoCRM(this, userInfo, parentSerial);

    }

}

 

public class VoIPProduct : IProduct

{

    private ILicense _license;

    private ILogging _logging;

    private bool _logLead = false;

 

    public VoIPProduct()

    {

        _license = new PluginLicense();

        _logging = new PluginLogging();

    }

 

    public VoIPProduct(bool logLead)

    {

        _license = new PluginLicense();

        _logging = new PluginLogging();

        _logLead = logLead;

    }

 

    public virtual void AddLicense(UserInfo userInfo, string parentSerialNumber)

    {

        _license.AddLicense(this, userInfo, parentSerialNumber);

    }

 

    public virtual int AddLicenseStatus(UserInfo userInfo)

    {

        return _license.AddLicenseStatus(this, userInfo);

    }

 

    public virtual void AddLeadIntoCRM(UserInfo userInfo, string parentSerial)

    {

        if (_logLead)

            _logging.AddLeadIntoCRM(this, userInfo, parentSerial);

    }

}

 

public interface ILicense

{

    void AddLicense(IProduct product, UserInfo userInfo, string parentSerialNumber);

    int AddLicenseStatus(IProduct product, UserInfo userInfo);

}

 

public interface ILogging

{

    void AddLeadIntoCRM(IProduct product, UserInfo userInfo, string parentSerial);

}

 

public class ParentLicense : ILicense

{

    public virtual void AddLicense(IProduct product, UserInfo userInfo, string parentSerialNumber)

    {

        webService.webService service = new webService.webService();

        string offerNumber = "XXXX-XXXX";

        string createLicense = service.AddLicense(offerNumber, "");

        string serialNumber = createLicense;

        product.SerialNumber = serialNumber.Remove(15, 8);

    }

 

    public virtual int AddLicenseStatus(IProduct product, UserInfo userInfo)

    {

        if (product.SerialNumber.ToLower().Contains("error"))

        {

            Utility.CreateErrorEmail(product, userInfo);

            return 5;

        }

        return 0;

    }

}

 

public class PluginLicense : ILicense

{

    public virtual void AddLicense(IProduct product, UserInfo userInfo, string parentSerialNumber)

    {

        webService.webService service = new webService.webService();

        string offerNumberPlugin = "XXXX-XXXX";

        string createLicensePlugin = service.AddLicense(offerNumberPlugin, parentSerialNumber);

        string serialNumberPlugin = createLicensePlugin;

        product.SerialNumber = serialNumberPlugin.Remove(15, 8);

    }

 

    public virtual int AddLicenseStatus(IProduct product, UserInfo userInfo)

    {

        if (product.SerialNumber.ToLower().Contains("error"))

        {

            Utility.CreateErrorEmail(product, userInfo);

            return 1;

        }

        return 0;

    }

}

 

public class ParentLogging : ILogging

{

    public virtual void AddLead(IProduct product, UserInfo userInfo, string parentSerial)

    {

        string serial = product.SerialNumber;

        string productName = product.ProductName + " Evaluation";

        webService.webService service = new webService.webService();

        string response = service.InsertLead(

            serial, parentSerial, "", "", "", "", "",

            "", "", "", "", "", "", "", "", "", "",

            "", "", "", "", "", "", "", "", "", "",

            "", "", "");

    }

}

 

public class PluginLogging : ILogging

{

    public virtual void AddLead(IProduct product, UserInfo userInfo, string parentSerial)

    {

        string serial = product.SerialNumber;

        string productName = product.ProductName + " Evaluation";

        webService.webService service = new webService.webService();

        string response = service.InsertLead(

            serial, parentSerial, "", "", "", "", "",

            "", "", "", "", "", "", "", "", "", "",

            "", "", "", "", "", "", "", "", "", "",

            "", "", "");

    }

}

Tags: