Factory Pattern

Factory Pattern is a creational patter. It provide encapsulation by hiding the complex components and their dependencies from the client.
Why do we need?
1. A class can’t anticipate the class of object it must create.
2. Client don’t need to use lot of new keywords.
3. Classes delegate responsibility to one of several helper subclasses.
4. If you want to develop a data layer that supports many types of database products, it should not be tightly coupled with a particular database product or ADO.NET data provider.

Ex: we will be creating an Employee objects. As with good OO design we will start with an interface.

public interface IEmployee
{
string GetDepartment();
string GetEducation();
}

Now Concrete Classes: we define two class inheriting from IEmployee interface i.e CTechnology & CHumanResource

public Class CTechnology : IEmployee
{
public string GetDepartment()
{
return “IT Department”;
}

public string GetEducation()
{
return “Engineering”;
}
}

public class CHumanResource : IEmployee
{
public string GetDepartment()
{
return “HR Department”;
}

public string GetEducation()
{
return “MBA”;
}
}

Now factory class, it is static and contain one method to return the correct object.
public class EmployeeFactory
{
public enum EmployeeType
{
IT, HR;
}

public static IEmployee CreateEmployee(EmployeeType empType)
{
switch(empType)
{
case EmployeeType.IT:
return new CTechnology;
break;

case EmployeeType.HR:
return new CHumanResource;
break;

default:
throw new ArgumentException(“Invalid Type of Employee”);
}
}
}

Now Client section: there are two buttons “IT EMployee” & “HR Employee” and two labels lblDept & lblEdu.

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnITEmployee_Click(object sender, EventArgs e)
{
IEmployee emp = EmployeeFactory.CreateEmployee(EmployeeFactory.EmployeeType.IT);
lblDept.Text = emp.GetDepartment();
lblEdu.Text = emp.GetEducation();
}

protected void btnHREmployee_Click(object sender, EventArgs e)
{
IEmployee emp =               EmployeeFactory.CreateEmployee(EmployeeFactory.EmployeeType.HR);
lblDept.Text = emp.GetDepartment();
lblEdu.Text = emp.GetEducation();
}

public interface IEmployee
{
string GetDepartment();
string GetEducation();
}Now we define two class inheriting from IEmployee interface i.e CTechnology & CHumanResource
public Class CTechnology : IEmployee
{
public CTechnology()
{

}
public string GetDepartment()
{
return “IT Department”;
}

public string GetEducation()
{
return “Engineering”;
}
}
public class CHumanResource : IEmployee
{
public CHumanResource()
{

}
public string GetDepartment()
{
return “HR Department”;
}

public string GetEducation()
{
return “MBA”;
}
}

Now our factory class, it is static and contain one method to return the correct object.
public class EmployeeFactory
{
public enum EmployeeType
{
IT, HR;
}

public static IEmployee CreateEmployee(EmployeeType empType)
{
switch(empType)
{
case EmployeeType.IT:
return new CTechnology;
break;

case EmployeeType.HR:
return new CHumanResource;
break;

default:
throw new ArgumentException(“Invalid Type of Employee”);
}
}
}

Advertisements

Leave a comment

Filed under .Net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s