Please enable Javascript to correctly display the contents on Dot Net Tricks!

Abstract Factory Design Pattern - C#

 Print 
  Author : Shailendra Chauhan
Posted On : 02 Jun 2013
Total Views : 89,235   
Updated On : 26 Sep 2016
 

Abstract Factory method pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is used to create a set of related objects, or dependent objects. Internally, Abstract Factory use Factory design pattern for creating objects. It may also use Builder design pattern and prototype design pattern for creating objects. It completely depends upon your implementation for creating objects. In this article, I would like share what is abstract factory pattern and how is it work?

What is Abstract Factory Pattern?

Abstract Factory patterns acts a super-factory which creates other factories. This pattern is also called as Factory of factories. In Abstract Factory pattern an interface is responsible for creating a set of related objects, or dependent objects without specifying their concrete classes.

Abstract Factory Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the abstract factory design pattern is given below:

The classes, interfaces and objects in the above UML class diagram are as follows:

  1. AbstractFactory

    This is an interface which is used to create abstract product

  2. ConcreteFactory

    This is a class which implements the AbstractFactory interface to create concrete products.

  3. AbstractProduct

    This is an interface which declares a type of product.

  4. ConcreteProduct

    This is a class which implements the AbstractProduct interface to create product.

  5. Client

    This is a class which use AbstractFactory and AbstractProduct interfaces to create a family of related objects.

C# - Implementation Code

public interface AbstractFactory
{
 AbstractProductA CreateProductA();
 
 AbstractProductB CreateProductB();
}
 
public class ConcreteFactoryA : AbstractFactory
{
 public AbstractProductA CreateProductA()
 {
 return new ProductA1();
 }
 
 public AbstractProductB CreateProductB()
 {
 return new ProductB1();
 }
}
 
public class ConcreteFactoryB : AbstractFactory
{
 public AbstractProductA CreateProductA()
 {
 return new ProductA2();
 }
 
 public AbstractProductB CreateProductB()
 {
 return new ProductB2();
 }
}
 
public interface AbstractProductA { }
 
public class ProductA1 : AbstractProductA { }
 
public class ProductA2 : AbstractProductA { }
 
public interface AbstractProductB { }
 
public class ProductB1 : AbstractProductB { }
 
public class ProductB2 : AbstractProductB { }

public class Client
{
 private AbstractProductA _productA;
 private AbstractProductB _productB;
 
 public Client(AbstractFactory factory)
 {
 _productA = factory.CreateProductA();
 _productB = factory.CreateProductB();
 }
}

Abstract Factory Pattern - Example

Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:

  1. VehicleFactory - AbstractFactory interface

  2. HondaFactory & HeroFactory- Concrete Factories

  3. Bike & Scooter - AbstractProduct interface

  4. Regular Bike, Sports Bike, Regular Scooter & Scooty - Concreate Products

  5. VehicleClient - Client

C# - Sample Code

/// <summary>
/// The 'AbstractFactory' interface. 
/// </summary>
interface VehicleFactory
{
 Bike GetBike(string Bike);
 Scooter GetScooter(string Scooter);
}

/// <summary>
/// The 'ConcreteFactory1' class.
/// </summary>
class HondaFactory : VehicleFactory
{
 public Bike GetBike(string Bike)
 {
 switch (Bike)
 {
 case "Sports":
 return new SportsBike();
 case "Regular":
 return new RegularBike();
 default:
 throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Bike));
 }

 }

 public Scooter GetScooter(string Scooter)
 {
 switch (Scooter)
 {
 case "Sports":
 return new Scooty();
 case "Regular":
 return new RegularScooter();
 default:
 throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Scooter));
 }

 }
}

/// <summary>
/// The 'ConcreteFactory2' class.
/// </summary>
class HeroFactory : VehicleFactory
{
 public Bike GetBike(string Bike)
 {
 switch (Bike)
 {
 case "Sports":
 return new SportsBike();
 case "Regular":
 return new RegularBike();
 default:
 throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Bike));
 }

 }

 public Scooter GetScooter(string Scooter)
 {
 switch (Scooter)
 {
 case "Sports":
 return new Scooty();
 case "Regular":
 return new RegularScooter();
 default:
 throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Scooter));
 }

 }
}

/// <summary>
/// The 'AbstractProductA' interface
/// </summary>
interface Bike
{
 string Name();
}

/// <summary>
/// The 'AbstractProductB' interface
/// </summary>
interface Scooter
{
 string Name();
}

/// <summary>
/// The 'ProductA1' class
/// </summary>
class RegularBike : Bike
{
 public string Name()
 {
 return "Regular Bike- Name";
 }
}

/// <summary>
/// The 'ProductA2' class
/// </summary>
class SportsBike : Bike
{
 public string Name()
 {
 return "Sports Bike- Name";
 }
}

/// <summary>
/// The 'ProductB1' class
/// </summary>
class RegularScooter : Scooter
{
 public string Name()
 {
 return "Regular Scooter- Name";
 }
}

/// <summary>
/// The 'ProductB2' class
/// </summary>
class Scooty : Scooter
{
 public string Name()
 {
 return "Scooty- Name";
 }
}

/// <summary>
/// The 'Client' class 
/// </summary>
class VehicleClient
{
 Bike bike;
 Scooter scooter;

 public VehicleClient(VehicleFactory factory, string type)
 {
 bike = factory.GetBike(type);
 scooter = factory.GetScooter(type);
 }

 public string GetBikeName()
 {
 return bike.Name();
 }

 public string GetScooterName()
 {
 return scooter.Name();
 }

}

/// <summary>
/// Abstract Factory Pattern Demo
/// </summary>
class Program
{
 static void Main(string[] args)
 {
 VehicleFactory honda = new HondaFactory();
 VehicleClient hondaclient = new VehicleClient(honda, "Regular");
 
 Console.WriteLine("******* Honda **********");
 Console.WriteLine(hondaclient.GetBikeName());
 Console.WriteLine(hondaclient.GetScooterName());
 
 hondaclient = new VehicleClient(honda, "Sports");
 Console.WriteLine(hondaclient.GetBikeName());
 Console.WriteLine(hondaclient.GetScooterName());
 
 VehicleFactory hero = new HeroFactory();
 VehicleClient heroclient = new VehicleClient(hero, "Regular");
 
 Console.WriteLine("******* Hero **********");
 Console.WriteLine(heroclient.GetBikeName());
 Console.WriteLine(heroclient.GetScooterName());
 
 heroclient = new VehicleClient(hero, "Sports");
 Console.WriteLine(heroclient.GetBikeName());
 Console.WriteLine(heroclient.GetScooterName());
 
 Console.ReadKey();
 }
}

Abstract Factory Pattern Demo - Output

When to use it?

  1. Create a set of related objects, or dependent objects which must be used together.

  2. System should be configured to work with multiple families of products.

  3. The creation of objects should be independent from the utilizing system.

  4. Concrete classes should be decoupled from clients.

Note

  1. Internally, Abstract Factory use Factory design pattern for creating objects. But it can also use Builder design pattern and prototype design pattern for creating objects. It completely depends upon your implementation for creating objects.

  2. Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.

  3. When Abstract Factory, Builder, and Prototype define a factory for creating the objects, we should consider the following points :

    1. Abstract Factory use the factory for creating objects of several classes.

    2. Builder use the factory for creating a complex object by using simple objects and a step by step approach.

    3. Prototype use the factory for building a object by copying an existing object.

What do you think?

I hope you will enjoy the Abstract Factory Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.



Free Interview Books
 
COMMENTS
Saket Kumar Reply 226 days 20 hours 37 mins ago

I have one question regarding this design pattern. The definition says, " In Abstract Factory pattern an interface is responsible for creating a set of related objects, or dependent objects without specifying their concrete classes." What I could not understand is why does it have to be related or dependent objects to be able to use this design pattern? Let's say I have two entirely different interfaces, IDeviceManager and ICountryManager, I will still be able to use this pattern seamlessly. Why does the definition states so regarding the relationship of objects? Could you please elaborate on this part?

18 NOV
Angular with Firebase (Classroom)
04:00 PM-07:00 PM IST(+5:30)
18 NOV
Angular2+4 with Firebase (Online)
10:30 AM-12:30 PM IST(+5.30 GMT)
11 NOV
ASP.NET MVC (Online)
08:00 AM-10:00 AM IST / 09:30 PM To 11:30 PM(CST)
11 NOV
ASP.NET MVC (Classroom)
09:00 AM to 12:00 AM
7 NOV
Developing Microsoft Azure Solutions (Online)
07:00 AM-08:30 AM IST/ 8:30 Pm -10:00 PM CST
14 OCT
Angular2 and Angular4 (Online)
03:00 PM-05:00 PM IST (+5.30GMT)
12 OCT
ASP.NET Core (Online)
09:00 PM - 11:00 PM IST(+5:30 GMT)
10 OCT
Microsoft Azure Infrastructure Solutions (Online)
08:00 AM-09:30 AM IST / 09:30 PM -11:00 PM CST
LIKE US ON FACEBOOK
 
+