Factory and Abstract Factory Pattern

Factory Pattern: When a method returns instance one of several possible classes on runtime, bases on input parameters. All these classes must share a common super class.

Factoy Pattern falls under Creational Design Pattern.

Example: Code is also available on GitHub. Click heto go to git hub repository

class Program

{

static void Main(string[] args)

{

var creator = new CarCreator();

 

// get Mercedes

var mercedes=creator.CreateCar(“Mercedes”);

Console.WriteLine(mercedes.AssembleCar());

 

// get BMW

var bmw = creator.CreateCar(“BMW”);

Console.WriteLine(bmw.AssembleCar());

 

// get OtherCar

var other = creator.CreateCar(“Other”);

Console.WriteLine(other.AssembleCar());

 

Console.ReadLine();

}

}

public class CarCreator

{

public Car CreateCar(string typeOfCar)

{

if (typeOfCar == “Mercedes”)

return new Mercedes();

else if (typeOfCar == “BMW”)

return new BMW();

else return new OtherCar();

}

}

 

public abstract class Car

{

public abstract string AssembleCar();

}

 

public class Mercedes : Car

{

public override string AssembleCar()

{

return “Mercedes assembled”;

}

}

public class BMW : Car

{

public override string AssembleCar()

{

return “BMW assembled”;

}

}

 

/// <summary>

/// OtherCar

/// </summary>

public class OtherCar : Car

{

/// <summary>

///

/// </summary>

/// <returns>String Other Car</returns>

public override string AssembleCar()

{

return “Other Car is assembled”;

}

}

Output result is

console
FactoryPattern output result

Abstract Factory Pattern: Abstract Factory pattern is one step ahead in encapsulation where there a method return the creator of one of several factories.

Abstract Factoy Pattern falls under Creational Design Pattern.

Example: Following is example

namespace AbstractFactoryPattern
{
class Program
{
static void Main(string[] args)
{
var factoryCreator = new FactoryCreator();

//Create Car Factory
var carFactory = factoryCreator.CreateFactory(“Car”);
Console.WriteLine($”Factory Created is {carFactory.GetType().Name}”);
var carCreator = carFactory.CreateVehicleCreator();

// Assemble Mercedes
var mercedes = carCreator.CreateVehicle(“Mercedes”);
Console.WriteLine(mercedes.AssembleVehicle());

// Assemble BMW
var bmw = carCreator.CreateVehicle(“BMW”);
Console.WriteLine(bmw.AssembleVehicle());

// Assemble otherCar
var otherCar = carCreator.CreateVehicle(“Other”);
Console.WriteLine(otherCar.AssembleVehicle());
// Creat Scooter Factory
var scooterFactory = factoryCreator.CreateFactory(“Scooter”);
Console.WriteLine($”Factory Created is {scooterFactory.GetType().Name}”);
var scooterCreator = scooterFactory.CreateVehicleCreator();

// Assemble Scooter A
var sccoterA = scooterCreator.CreateVehicle(“ScooterA”);
Console.WriteLine(sccoterA.AssembleVehicle());

// Assemble Scooter A
var sccoterB = scooterCreator.CreateVehicle(“ScooterB”);
Console.WriteLine(sccoterB .AssembleVehicle());

Console.ReadLine();
}
}

public class FactoryCreator
{
/// <summary>
///
/// </summary>
/// <param name=”typeOfVehicle”></param>
/// <returns></returns>
public IFactory CreateFactory(string typeOfVehicle)
{
if (typeOfVehicle==”Car”)
return new CarFactory();
else
return new ScooterFactory();
}
}

/// <summary>
///
/// </summary>
public class ScooterFactory:IFactory
{

public IVehicleCreator CreateVehicleCreator()
{
return new ScooterCreator();
}
}

/// <summary>
///
/// </summary>
public class CarFactory : IFactory
{
public IVehicleCreator CreateVehicleCreator()
{
return new CarCreator();
}
}

/// <summary>
///
/// </summary>
public interface IFactory
{
IVehicleCreator CreateVehicleCreator();
}

public interface IVehicleCreator
{
IVehicle CreateVehicle(string vehilceType);
}

public interface IVehicle
{
string AssembleVehicle();
}
public class CarCreator:IVehicleCreator
{
public IVehicle CreateVehicle(string typeOfCar)
{
if (typeOfCar == “Mercedes”)
return new Mercedes();
else if (typeOfCar == “BMW”)
return new BMW();
else return new OtherCar();
}
}
/// <summary>
///
/// </summary>
public class BMW : Car,IVehicle
{
public string AssembleVehicle()
{
return “BMW assembled”;
}
}

/// <summary>
/// Other Car
/// </summary>
public class OtherCar : Car,IVehicle
{
/// <summary>
///
/// </summary>
/// <returns></returns>
public string AssembleVehicle()
{
return “OtherCar assembled”;
}
}

/// <summary>
/// Mercedes
/// </summary>
public class Mercedes : Car,IVehicle
{
public string AssembleVehicle()
{
return “Mercedes assembled”;
}
}

/// <summary>
/// Abstract class Car
/// </summary>
public abstract class Car
{
}

/// <summary>
/// ScooterCreator
/// </summary>
public class ScooterCreator:IVehicleCreator
{
public IVehicle CreateVehicle(string typeOfScooter)
{
if (typeOfScooter == “ScooterA”)
return new ScooterA();
else if (typeOfScooter == “ScooterB”)
return new ScooterB();
else return new OtherScooter();
}
}
/// <summary>
/// Other Scooter
/// </summary>
public class OtherScooter : Scooter,IVehicle
{
public string AssembleVehicle()
{
return “OtherScooter assembled”;
}
}

/// <summary>
/// ScooterB
/// </summary>
public class ScooterB : Scooter,IVehicle
{
public string AssembleVehicle()
{
return “ScooterB assembled”;
}
}

/// <summary>
/// ScooterA
/// </summary>
public class ScooterA : Scooter,IVehicle
{
public string AssembleVehicle()
{
return “ScooterA assembled”;
}
}
/// <summary>
/// abstarct class Scooter
/// </summary>
public abstract class Scooter
{
}
}

console
Abstract Factory Pattern Output result

 

 

Author: Rupesh

Hi! I'm Rupesh, a funophile and technophile. I'm an Application Developer, Solution Architect and IT-Consultant, and an author in the works. I am a Microsoft certified Professional and Solution Developer (MCP and MCSD).

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