Abstract Factory Method:
Abstract factory method created the object of related
classes or dependent classes without specifying their concrete class.
Gang of four (GoF) defination:
According to the Gof the abstract factory pattern provide an interface for creating families of related or dependent objects without specifying their concrete classes.
Where to use it:
Abstract factory method can be used in the following
scenarios:
- When a system should be independent of how its products are
created, composed, and represented.
- A class can not anticipate the class of object it must
create
- A system must use just one of a set of families of product.
Advantage:
One of the main advantages of Abstract Factory Pattern is that it isolates the
concrete classes that are generated. The names of actual implementing classes
are not needed to be known at the client side. Because of the isolation, you
can change the implementation from one factory to another.
Example:
I am giving a very simple example. When I was studying C++ in college then our teacher taught us a concept of polymorphism by giving a very simple example. btw the polymorphism is that " The same function call behaves differently on every call"..I practically implemented polymorphism in C++ by using function overrding and inheritance. The assignment was that We have a living things all around us which includes human being, plant etc. Human beings can walk but the plant can not walk. So to solve this problem I made a c++ example in which I made an abstract class of Living thing and then inherited two class from it (i.e. Plant and Human being). Then I override the CanWalk method of the abstract class in the child classes with their own body.
Today I will take the same example for the abstract factory method. My code of the abstract factory method is given below and also the .cs file is attached at the end of this post.
using System;
namespace Shahid
{
// MainApp test application
class MainApp
{
public static void Main()
{
LivingFactory factory = new ConcreteLiving1();
Client c1 = new Client(factory);
c1.Run();
LivingFactory factory2 = new ConcreteLiving2();
Client c2 = new Client(factory2);
c2.Run();
}
}
// "AbstractFactory whicg is Living Factory"
abstract class LivingFactory
{
public abstract AbstractLivingAnimal CreateLivingAnimal();
public abstract AbstractLivingPlant CreateLivingPlant();
}
// "ConcreteLiving1"
class ConcreteLiving1 : LivingFactory
{
public override AbstractLivingAnimal CreateLivingAnimal()
{
return new LivingAnimal1();
}
public override AbstractLivingPlant CreateLivingPlant()
{
return new LivingPlant1();
}
}
// "ConcreteLiving2"
class ConcreteLiving2 : LivingFactory
{
public override AbstractLivingAnimal CreateLivingAnimal()
{
return new LivingAnimal2();
}
public override AbstractLivingPlant CreateLivingPlant()
{
return new LivingPlant2();
}
}
// "Abstract Living Animal
abstract class AbstractLivingAnimal
{
public abstract void CanWalk(AbstractLivingPlant plant);
}
// "AbstractLiving plant"
abstract class AbstractLivingPlant
{
}
// "Living Animal 1"
class LivingAnimal1 : AbstractLivingAnimal
{
public override void CanWalk(AbstractLivingPlant plant)
{
Console.WriteLine(this.GetType().Name + " can walk but " + plant.GetType().Name + " can not");
}
}
// "Living Plant 1"
class LivingPlant1 : AbstractLivingPlant
{
}
class LivingAnimal2 : AbstractLivingAnimal
{
public override void CanWalk(AbstractLivingPlant plant)
{
Console.WriteLine(this.GetType().Name + " can walk but " + plant.GetType().Name + " can not");
}
}
// "Living plant 2"
class LivingPlant2 : AbstractLivingPlant
{
}
// "Client" - the client code
class Client
{
private AbstractLivingAnimal Animal;
private AbstractLivingPlant Plant;
// Constructor
public Client(LivingFactory livingThing)
{
Animal = livingThing.CreateLivingAnimal();
Plant = livingThing.CreateLivingPlant();
}
public void Run()
{
Animal.CanWalk(Plant);
}
}
} |
In the above example we can see that in the main program we didn't called the LivingAnimal1,LivingAnimal2,LivingPlant1 and LivingPlant3 class directly. The code is given below:
LivingFactory factory = new ConcreteLiving1();
Client c1 = new Client(factory);
c1.Run();
LivingFactory factory2 = new ConcreteLiving2();
Client c2 = new Client(factory2);
c2.Run();
In the above code you can see that we have created the instance of an abstract class LivingFactory and instantiated it by the ConcreteLiving1 class. In the code you can see that the ConcreteLiving class is inherited from the abstract class LivingFactory. Then in the next line we created the object of the Client class and then called the Run method of the Client class. The Client's constructor take the parameter of Type LivingFactory and produces the output according to the parameter passed to it. But in the main program we dont know that what will be the out put of Client.Run(). Also if your business logic changes then you can change theimplementation of the Factory classes witout affecting the client side code.
In the example we can see that our main program is independent that how the LivingAnimal1,LivingAnimal2,LivingPlant1 and LivingPlant2 are created and composed.
Regards,
Shahid Riaz Bhatti
Microsoft certified professional
Program.cs (2.86 kb)