.Net, Design Patterns

Abstract Factory Pattern in C#

Introduction

According to Wikipedia:

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.

According to Gang Of Four:

Provide an interface for creating families of related or dependent objects without
specifying their concrete classes.

Use of this pattern makes it possible to interchange concrete implementations without changing the code that uses them, even at runtime.

UML:

677px-Abstract_factory_UML.svg

When to use it?

  • When the system is independent on how the objects are created.
  • When the system should work with multiple families of product.
  • When an interface is required to create similar family products, not the implementation.

Why to use it?

  • Provides an interface for creating objects which are related or dependent objects without specifying concrete classes
  • Calling code works with abstract interface and classes and does not need to know the exact types of the objects created by the factory

The main players in the pattern are:

  • AbstractFactory – declares a interface for operations that create abstract products.
  • ConcreteFactory – implements operations to create concrete products.
  • AbstractProduct – declares an interface for a type of product objects.
  • Product – defines a product to be created by the corresponding ConcreteFactory; it implements the AbstractProduct interface.
  • Client – uses the interfaces declared by the AbstractFactory and AbstractProduct classes.

Example in C#

ComputerFactory.cs (Abstract Factory)


namespace DesignPatterns.AbstractPattern {
abstract class ComputerFactory {
public abstract Computer GetComputer();

}//ComputerFactory
}

ConcreteComputerFactory.cs (ConcreteFactory)


namespace DesignPatterns.AbstractPattern {
class ConcreteComputerFactory : ComputerFactory {

public override Computer GetComputer() {

return new ConcreteComputer();

}//GetComputer

}//ConcreteComputerFactory
}

Computer.cs (Abstract Product)


namespace DesignPatterns.AbstractPattern {
abstract class Computer {

public abstract int Mhz { get; }

}//Computer
}

ConcreteComputer.cs (Product)


namespace DesignPatterns.AbstractPattern {
class ConcreteComputer : Computer {

int _mhz = 500;

public override int Mhz{
get { return _mhz; }
}//Mhz

}//ConcreteComputer
}

Client Code


using System;

namespace DesignPatterns.AbstractPattern {
class AbstractFactoryPatternClient : IDesignPattern {
public void Run() {
Console.WriteLine("\n------------------Abstract Factory Pattern------------------");
ComputerFactory factory = new ConcreteComputerFactory();

new ComputerAssembler().AssembleComputer(factory);
}
}
}

ComputerAssembler.cs


using System;

namespace DesignPatterns.AbstractPattern {
class ComputerAssembler {
public void AssembleComputer(ComputerFactory factory) {

Computer computer = factory.GetComputer();
Console.WriteLine("Assembled a {0} running at {1} MHz",
computer.GetType().FullName, computer.Mhz);

}//AssembleComputer
}
}

Output:

Assembled a DesignPatterns.AbstractPattern.ConcreteComputer running at 500 MHz

So whats happening here?

If you see the client code its passing an Abstract factory to the ComputerAssembler class and this class has a public method AssembleComputer which takes the Abstract factory class and calls the exposed public method of that factory.

So good thing here is that the AssembleComputer does not care about any specific object but it can support any type of ComputerFatcory which in this example its the ConcreteComputerFacory which implements ComputerFactory; and then it calls the GetComputerMethod which returns a concrete computer which implements Computer abstract class, which again decouples it from any concrete class.

Basically idea is to decouple the implementation logic to any specific concrete class, in this way every concrete class will have an common interface to implement and the client can call any method of the Product without worrying the exact object type.

Code can be found at GitHub

Reference:

 

 

One thought on “Abstract Factory Pattern in C#

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s