Saturday, April 6, 2013

Factory Design Pattern C# Example

Factory it's such a Design Pattern which defines an interface for creating an object, but lets the classes that implement the interface decide which class to instantiate. Factory Pattern lets a class postpone instantiation to sub-classes.

Let's see Factory Pattern in action on a simple example. Assume you have two different classes Toyota and Suzuki, they both implements abstract class Car. You need to instantiate one of these classes, but you don't know which of them, it depends on user. This is perfect scenario for the Factory Pattern.

Factory Design Pattern example program diagram


So here are our two main classes and the interface (abstract class):
abstract class Car
{
    public abstract string Company { get; }
    public abstract string Model { get; set; }
    public abstract int Speed { get; set; }
}

class Suzuki : Car
{
    private readonly string _company;
    private string _model;
    private int _speed;

    public Suzuki(string model, int speed)
    {
        _company = "Suzuki";
        _model = model;
        _speed = speed;
    }

    public override string Company
    {
        get { return _company; }
    }

    public override string Model
    {
        get { return _model; }
        set { _model = value; }
    }

    public override int Speed
    {
        get { return _speed; }
        set { _speed = value; }
    }
}

class Toyota : Car
{
    private readonly string _company;
    private string _model;
    private int _speed;

    public Toyota(string model, int speed)
    {
        _company = "Toyota";
        _model = model;
        _speed = speed;
    }

    public override string Company
    {
        get { return _company; }
    }

    public override string Model
    {
        get { return _model; }
        set { _model = value; }
    }

    public override int Speed
    {
        get { return _speed; }
        set { _speed = value; }
    }
}
Abstract CarFactory class is an interface for our ToyotaFactory and SuzukiFactory classes that will be responsible for the instantiation of corresponding car, therefore they only have one single GetCar() method.
abstract class CarFactory
{
    public abstract Car GetCar();
}

class ToyotaFactory : CarFactory
{
    private string _model;
    private int _speed;

    public ToyotaFactory(string model, int speed)
    {
        _model = model;
        _speed = speed;
    }
    public override Car GetCar()
    {
        return new Toyota(_model, _speed);
    }
}

class SuzukiFactory: CarFactory
{
    private string _model;
    private int _speed;

    public SuzukiFactory(string model, int speed)
    {
        _model = model;
        _speed = speed;
    }

    public override Car GetCar()
    {
        return new Suzuki(_model, _speed);
    }
}
What is left is only CarAssembler class which will create needed type of car depending on the factory that it receives as parameter.
class CarAssembler
{
    public void AssembleCar(CarFactory factory)
    {
        Car car = factory.GetCar();
        Console.WriteLine("{0} {1} assembled successfully. Maximum speed is {2} mph", 
            car.Company, car.Model, car.Speed);
    }
}
That's all! Now you can instantiate corresponding car object depending on user input:
static void Main(string[] args)
{
    CarFactory factory = null;
    Console.WriteLine("Which car you would like to build?");
    string car = Console.ReadLine();
    if (car == "toyota")
        factory = new ToyotaFactory("Corolla", 130);
    else
        factory = new SuzukiFactory("Swift", 110);

    new CarAssembler().AssembleCar(factory);
}
Here is the program output:

Factory Pattern example output

Download the source code (Visual Studio 2012 project).

13 comments:

  1. whats difference between factory and abstract Factory Pattern .

    ReplyDelete
  2. With the Factory pattern, you produce implementations (Apple, Banana, Cherry, etc.) of a particular interface let's say, IFruit.

    With the Abstract Factory pattern, you produce implementations of a particular Factory interface, let's say IFruitFactory. Each of those knows how to create different kinds of fruit.

    ReplyDelete