Abstract Factory Pattern In C#


Definition

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

UML class diagram


Participants

The classes and objects participating in this pattern are:

  • AbstractFactory  (ContinentFactory)
    • declares an interface for operations that create abstract products
  • ConcreteFactory   (AfricaFactory, AmericaFactory)
    • implements the operations to create concrete product objects
  • AbstractProduct   (Herbivore, Carnivore)
    • declares an interface for a type of product object
  • Product  (Wildebeest, Lion, Bison, Wolf)
    • defines a product object to be created by the corresponding concrete factory
    • implements the AbstractProduct interface
  • Client  (AnimalWorld)
    • uses interfaces declared by AbstractFactory and AbstractProduct classes

Structural code in C#

This structural code demonstrates the Abstract Factory pattern creating parallel hierarchies of objects. Object creation has been abstracted and there is no need for hard-coded class names in the client code.

class Program
{
static void Main(string[] args)
{
AbstractFactory factory1 = new ConcreteFactory1();
Client client1 = new Client(factory1);
client1.Run();
AbstractFactory factory2 = new ConcreteFactory2();
Client client2 = new Client(factory2);
client2.Run();

Console.ReadKey();

}
}

abstract class AbstractProductA
{

}
class ProductA1 : AbstractProductA
{

}

class ProductA2 : AbstractProductA
{
}

abstract class AbstractProductB
{
public abstract void Interact(AbstractProductA a);
}
class ProductB1 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name + ” interact with ” + a.GetType().Name);
}
}

class ProductB2 : AbstractProductB
{
public override void Interact(AbstractProductA a)
{
Console.WriteLine(this.GetType().Name + ” interact with ” + a.GetType().Name);
}
}

abstract class AbstractFactory
{
public abstract AbstractProductA CreateProductA();
public abstract AbstractProductB CreateProductB();
}

class ConcreteFactory1 : AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA1();
}
public override AbstractProductB CreateProductB()
{
return new ProductB1();
}
}

class ConcreteFactory2 : AbstractFactory
{
public override AbstractProductA CreateProductA()
{
return new ProductA2();
}
public override AbstractProductB CreateProductB()
{
return new ProductB2();
}
}

class Client
{
private AbstractProductA abstractProductA;
private AbstractProductB absTractProductB;

public Client(AbstractFactory abs)
{
this.abstractProductA = abs.CreateProductA();
this.absTractProductB = abs.CreateProductB();

}

public void Run()
{
this.absTractProductB.Interact(this.abstractProductA);
}
}

Output
ProductB1 interacts with ProductA1
ProductB2 interacts with ProductA2

Real-world code in C#

This real-world code demonstrates the creation of different animal worlds for a computer game using different factories. Although the animals created by the Continent factories are different, the interactions among the animals remain the same.

static void Main()
{

ContinentFactory contiAfica = new AfricaFactory();
AnimalWorld animal = new AnimalWorld(contiAfica);
animal.RunFoodChain();
Console.ReadKey();
}

abstract class ContinentFactory
{
public abstract Herbivore CreateHerbivoer();
public abstract Carnivore CreateCarnivore();

}

class AfricaFactory : ContinentFactory
{
public override Carnivore CreateCarnivore()
{
return new Lion();
}

public override Herbivore CreateHerbivoer()
{
return new Wildebeest();
}
}
class AmericaFactory : ContinentFactory
{
public override Carnivore CreateCarnivore()
{
return new Worlf();
}
public override Herbivore CreateHerbivoer()
{
return new Bison();
}
}

abstract class Herbivore
{

}
abstract class Carnivore
{
public abstract void Eat(Herbivore h);
}

class Wildebeest : Herbivore
{

}

class Lion : Carnivore
{
public override void Eat(Herbivore h)
{
Console.WriteLine(this.GetType().Name + ” eats ” + h.GetType().Name);
}
}

class Bison : Herbivore
{

}
class Worlf : Carnivore
{
public override void Eat(Herbivore h)
{
Console.WriteLine(this.GetType().Name + ” eats ” + h.GetType().Name);
}
}

class AnimalWorld
{
private Herbivore _herbivover;
private Carnivore _carnivore;
public AnimalWorld(ContinentFactory conti)
{
this._carnivore = conti.CreateCarnivore();
this._herbivover = conti.CreateHerbivoer();
}
public void RunFoodChain ()
{
this._carnivore.Eat(this._herbivover);
}
}

Output
Lion eats Wildebeest
Wolf eats Bison
Advertisements

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