Factory Design Patterns

Hi All,

In this article, we will discuss about Factory design pattern

This is type of creational patterns i.e. it helps us to create objects.
The key feature is to create concrete subclasses

Factory Pattern is defined as “Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses”

Let’s consider the below issue & also we will see how to resolve this issue with design pattern
Lets say I am developing an application to Client, & client’s requirement is I should develop my application in such a way that if I enable config saying SQL my application should connect to SQL DB & if I enable config saying Oracle it should connect to Oracle.

This kind of issue can be resolved with Factory Design Pattern.
So to fix this I have to write all database related methods with respect SQL & Oracle. Also the connection to database should happen on config.

In this we will define Interface as IConnection, this define all the DB related methods


// Base Interface
public interface IConnection
{
void Display(string conString);
}

I will have class specific to SQL as shown below & this class has to implement IConnection


// A Connection Class that Implements the IConnection Interface
public class SqlConnect : IConnection
{
public void IConnection.Display(string conString)
{
// Method to display data by connecting to SQL Server DB
}
}

I will have class specific to Oracle as shown below & this class has to implement IConnection


// A Connection Class that Implements the IConnection Interface
public class OracleConnect : IConnection
{
public void IConnection.Display(string conString)
{
// Method to display data by connecting to Oracle Server DB
}
}

// The Connection Factory
public class ConnectionFactory
{
public static IConnection getConnection(string connectionType)
{
switch (connectionType)

{
case “Sql”:
return new SqlConnect();
case “Oracle”:
return new OracleConnect();
default:
throw new ApplicationException(string.Format(”ConnectionType ‘{0}’ cannot be created”, connectionType));
break;
}
}
}

Now Lets try with below example
Create a class & add the following code


public interface IConnection
{
string GetStudentById(int stdId);
}

public class SQlConnect : IConnection
{
public SQlConnect()
{
Console.WriteLine(”SQL Connection Class is Called!!!”);
}
public string GetStudentById(int stdId)
{
return “Student name is Mr. Prakash & his ID ” + stdId.ToString() + ” “;
}

}

public class OracleConnect : IConnection
{
public OracleConnect()
{
Console.WriteLine(”SQL Connection Class is Called!!!”);
}
public string GetStudentById(int stdId)
{
return “Student name is Mr. Prakash & his ID ” + stdId.ToString() + ” “;
}

}

Let’s create a web page & name the page as “FactoryPattern” and add the following code


public enum ConnectionType
{
Sql, Oracle
}

static FactoryPattern()
{
}
public static IConnection CreateConnectionObject(ConnectionType TypeOfConnection)
{
switch (TypeOfConnection)
{
case ConnectionType.Oracle:
return new OracleConnect();
case ConnectionType.Sql:
return new SQlConnect();
default:
throw new ArgumentException(”Invalid Connection Type!”);
}
}

In the page load event add the following code


IConnection connT;

if (ConfigurationManager.AppSettings[”ConnectionType”] == “1″)
{
connT = FactoryPattern.CreateConnectionObject(FactoryPattern.ConnectionType.Sql);
}
else
{
connT = FactoryPattern.CreateConnectionObject(FactoryPattern.ConnectionType.Oracle);

}

string str = connT.GetStudentById(1);

/* the above lines of codes clearly states that object return type is defined based on config entry only & in future we need to add another connection type we can still do it with lot minimal changes without affecting the existing changes */

Happy Koooding….. Hope this helps !!!!

Advertisement