Dec 16 2009

Overview of the Facade Design Pattern

Category: Design Patternryancmartin1976 @ 17:50

First Strategy:

Second Strategy:

Façade Design Pattern in C#

The Façade pattern is sometimes called and written as a class manager. It's an easy way to organize and unify numerous classes usually within the same namespace and design structure.

The Façade pattern is a high level object pulling together low level objects for simplifying application complexity.

public class Evaluate

{

    public int SubmitForm()

  {

        List<IProduct> products = new List<IProduct>();

      string productToEval = "premium";

        switch (productToEval)

        {

            case "standard":

                products.Add(new StandardProduct());

                break;

            case "premium":

                products.Add(new PremiumProduct());

                break;

            case "substandard":

                products.Add(new SubStandardCentralProduct());

                products.Add(new SubStandardRemoteProduct());

                break;

            case "distributed":

                products.Add(new DistributedCentralProduct());

                products.Add(new DistributedRemoteProduct());

                break;

        }

 

        /////////////////////////////////////////////

        // THEN ADD PLUGIN PRODUCT

        /////////////////////////////////////////////

        products.Add(new IntegratedProduct(false));

        products.Add(new ExternalProduct(false));

        products.Add(new DiscontinuedProduct(false));

 

        EvalFacade evalFacade = new EvalFacade(products, userInfo);

        return evalFacade.Run();

  }

}

 

public class EvalFacade

{

    private List<IProduct> _products;

    private UserInfo _userInfo;

 

    private string _parentSerialNumber = "";

    public string ParentSerialNumber

    {

        get { return _parentSerialNumber; }

        set { _parentSerialNumber = value; }

    }

 

    public EvalFacade(List<IProduct> products, UserInfo userInfo)

    {

        _products = products;

        _userInfo = userInfo;

    }

 

    public int Run()

    {

        int status = 0;

        foreach (IProduct product in _products)

        {

            if (product != null)

            {

                product.AddLicense(_userInfo, this.ParentSerialNumber);

                status = product.AddLicenseStatus(_userInfo);

                if (status != 0)

                    return status;

                product.AddLeadIntoCRM(_userInfo, this.ParentSerialNumber);

                if (string.IsNullOrEmpty(this.ParentSerialNumber) && !string.IsNullOrEmpty(product.SerialNumber))

                    this.ParentSerialNumber = product.SerialNumber;

            }

        }

        return status;

    }

}

Tags:

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:

Dec 16 2009

Overview of the Strategy Design Pattern

Category: Design Patternryancmartin1976 @ 17:01

First Strategy:

Second Strategy:

Strategy Design Pattern in C#

This is one of the most common design patterns used throughout software design.

The pattern has interchangeable child classes that derive from an abstract base class or an interface. The interface tells those classes what it is expecting for method signatures and properties.

public class Evaluate

{

    public void SubmitForm()

    {

        List<IProduct> products = new List<IProduct>();

        string productToEval = "premium";

        switch (productToEval)

        {

            case "standard":

                products.Add(new StandardProduct());

                break;

            case "premium":

                products.Add(new PremiumProduct());

                break;

            case "substandard":

                products.Add(new SubStandardCentralProduct());

                products.Add(new SubStandardRemoteProduct());

                break;

            case "distributed":

                products.Add(new DistributedCentralProduct());

                products.Add(new DistributedRemoteProduct());

                break;

        }

 

        products.Add(new IntegratedProduct(false));

        products.Add(new ExternalProduct(false));

        products.Add(new DiscontinuedProduct(false));

    }

}

 

public interface IProduct

{

    string BuildNumber { get; set; }

    string ProductName { get; set; }

 

    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 virtual string BuildNumber

    {

        get { return WebConfigurationManager.AppSettings["Premium_Build_Number"]; }

        set { }

    }

 

    public virtual string ProductName

    {

        get { return WebConfigurationManager.AppSettings["Premium_Product_Name"]; }

        set { }

    }

}

 

public class SubStandardRemoteProduct : IProduct

{

    private ILicense _license;

    private ILogging _logging;

 

    public SubStandardRemoteProduct()

    {

        _license = new RemoteLicense();

        _logging = new RemoteLogging();

    }

 

    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 virtual string BuildNumber

    {

        get { return WebConfigurationManager.AppSettings["SubStandard_Remote_Build_Number"]; }

        set { }

    }

 

    public virtual string ProductName

    {

        get { return WebConfigurationManager.AppSettings["SubStandard_Remote_Product_Name"]; }

        set { }

    }

}

 

public class ExternalProduct : IProduct

{

    private ILicense _license;

    private ILogging _logging;

    private bool _logLead = false;

 

    public ExternalProduct()

    {

        _license = new PluginLicense();

        _logging = new PluginLogging();

    }

 

    public ExternalProduct(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 virtual string BuildNumber

    {

        get { return WebConfigurationManager.AppSettings["External_Build_Number"]; }

        set { }

    }

 

    public virtual string ProductName

    {

        get { return WebConfigurationManager.AppSettings["External_Product_Name"]; }

        set { }

    }

}

Tags: