Friday, April 23, 2010

Useful Design Patterns

Recurring solutions for recurring problems are what we call a Design Pattern.
We are standing at a phase of time where we can say that software development has provided us with some efficient and sophisticated methods for problem solving. I would like to introduce some of the design patterns which I think should be used during software development.

1. Singleton Pattern
If you want a single instance of an entity within the entire application then go for the singleton pattern. Following is an example of a Singleton Pattern using C#.Net;


public class SingletonPattern
{
private static SingletonPattern objSingeltonPattern = null;
private SingletonPattern()
{
}

public static SingletonPattern GetInstance()
{
try
{
if (objSingeltonPattern == null)
return objSingeltonPattern = new SingletonPattern();
else
return objSingletonPattern;
}
catch (Exception)
{

throw;
}
}
}



2. Factory Pattern

As a software developer we are always looking for a mechanism that can mange the object creation process. OOP has indeed contributed a lot in the efficient software development of the systems but still we have to mess up with different types of objects in our system. If you are looking for a class or a particular method that can handle object creation process then Factory pattern is the one that will rescue you.
Following is a an example of Factory Pattern using an entity Customer.


public interface ICustomer
{
string GetCustomerType();
}

public class CompanyCustomer : ICustomer
{
public string GetCustomerType()
{
return "Company Customer";
}
}

public class IndividualCustomer : ICustomer
{
public string GetCustomerType()
{
return "Individual Customer";
}
}

public class FactoryClass
{
public enum CustomerTypes
{
CompanyCustomer,
IndividualCustomer
}

public ICustomer GetCustomerObject(CustomerTypes varCustomerType)
{
switch (varCustomerType)
{
case CustomerTypes.CompanyCustomer:
return new CompanyCustomer();
case CustomerTypes.IndividualCustomer:
return new IndividualCustomer();
}
return null;
}
}




2. Strategy Pattern

The wonderful and mysterious world of software development always wants us to solve a problem in a more efficient manner. We some times encounter certain types of problems that are slightly distinguish themselves with each other. Divide and Conquer is thumb rule for efficient software development. Decomposition of a problem always leads us to efficient solution of a problem. There are some types of problems that belongs to the same clan but still they have their own distinction.
Strategy Pattern is mostly used when we are in a position to change strategy dynamically. For Example while playing a game we often change our strategy to be succeed.A good use of the Strategy pattern would be saving files in different formats, running various sorting algorithms, or file compression.

Following is the an example of a Strategy Pattern implemented using C#.

public interface ICustomers
{
Double CalculateAmountWithSalesTax(Double Amount);
}

public class IndividualCustomers : ICustomers
{
public Double CalculateAmountWithSalesTax(Double Amount1)
{
return Amount1 + (Amount1 * 7) / 100;
}
}

public class CompanyCustomers : ICustomers
{
public Double CalculateAmountWithSalesTax(Double Amount2)
{
return Amount2 + (Amount2 * 15) / 100;
}
}

public class StrategyPattern
{
ICustomers objICustomer;

public ICustomers SetCustomerType
{
set
{
objICustomer = value;
}
}

public Double CalculateAmount(Double Amount)
{
return objICustomer.CalculateAmountWithSalesTax(Amount);
}
}


Following is the code for calling the preceding method(s);

private void btnStrategyPattern_Click(object sender, EventArgs e)
{
try
{
IndividualCustomers objIndividualCustomer = new IndividualCustomers();
CompanyCustomers objCompanyCustomer = new CompanyCustomers();

StrategyPattern objStrategyPattern = new StrategyPattern();
objStrategyPattern.SetCustomerType = objIndividualCustomer;

MessageBox.Show(objStrategyPattern.CalculateAmount(50).ToString(), "Strategy Pattern", MessageBoxButtons.OKCancel);

objStrategyPattern.SetCustomerType = objCompanyCustomer;
MessageBox.Show(objStrategyPattern.CalculateAmount(50).ToString(), "Strategy Pattern", MessageBoxButtons.OKCancel);
}
catch (Exception)
{

throw;
}
}



References:
1. http://aspalliance.com/827_Gang_of_Four_GOF_Design_Patterns.4
2. http://java.dzone.com/articles/design-patterns-strategy?utm_source=feedburner&utm_medium=feed&= utm_campaign=Feed%3A+zones%2Farchitects+%28Architects+Zone%29

No comments:

Post a Comment