Thursday, December 29, 2016

Factory Pattern

The Factory Pattern is a Creational pattern that uses a specialized object solely to create other objects. This pattern deals with the instantiation of objects without exposing the instantiation logic. In other words, a Factory is actually a creator of objects which have a common interface. It relies heavily on inheritance as object creation is delegated to subclasses that implement the factory method to create objects.

This creation of other objects happens by calling a factory method either :
  • By defining it in an interface and implement the same in child classes, (OR)
  • Implement it in base class and optionally override the same in derived classes by eliminating the constructor call
Factory pattern tries to address:
  • eliminating significant duplication of code
  • required information not accessible to the composing object
  • may not provide sufficient level of abstraction
  • may otherwise not be part of the composing object's concerns

Advantages: Factory classes are often implemented because they allow the project to follow Interface Segregation and Dependency Inversion SOLID principles more closely. Some of the advantages of using this pattern are as below:

  • It allows you to introduce an Inversion of Control (IoC) container easily
  • It makes your code more testable as you can mock interfaces
  • It gives you a lot more flexibility when it comes time to change the application (i.e. create new implementations without changing the dependent code)

Example for "Defining it in an interface and implement the same in child classes"
//Interface definition of actual object
public interface IEmployee
{
string GetEmployeeStatus(int empId);
}

public class Employee : IEmployee
{
public string GetEmployeeStatus(int empId)
{
return "Employee";
}
}

public class ContractEmployee : IEmployee
{
public string GetEmployeeStatus(int empId)
{
return "Contract Employee";
}
}

public enum EmployeeType
{
Employee,
Contractor
}

/// Implementation of Factory - Used to create objects
public class Factory
{
public IEmployee GetEmployee(EmployeeType empType)
{
switch (empType)
{
case EmployeeType.Employee:
return new Employee();
case PersonType.Contractor:
return new ContractEmployee();
default:
throw new NotSupportedException();
}
}
}
Example for "Implement Factory Method in Base class and optionally override the same in Derived classes"
public interface IProduct
{
string GetName();
string SetPrice(double price);
}

public class Phone : IProduct
{
private double _price;
public string GetName()
{
return "Apple iPhone";
}

public string SetPrice(double price)
{
this._price = price;
return "success";
}
}

/* Almost same as Factory, just an additional exposure to do something with the created method */
public abstract class ProductAbstractFactory
{
protected abstract IProduct DoSomething();

public IProduct GetObject() // Implementation of Factory Method.
{
return this.DoSomething();
}
}

public class PhoneConcreteFactory : ProductAbstractFactory
{
protected override IProduct DoSomething()
{
IProduct product = new Phone();
//Do something with the object after you get the object.
product.SetPrice(20.30);
return product;
}
}
With this I am concluding the illustration. Feel free to share your feedback.

Happy Programming !!!

No comments:

Post a Comment