By using Abstract Interfaces the concrete implemented classes are encapsulated (Hidden) in this abstraction layer from the client.
Example:
The following is an example on how the Abstract Factory is used to create a family of related objects.
Basically we are a Factory to create Planets, Intelligent Life and some sort of Food.
The client never gets exposed to the Concrete Products. i.e.: Humans or Cows etc.
This is the way it should be. Imagine if the Food type Cow goes extinct, the client would need to get into the code that creates Cows and modify it to create a new type of Food. But by using the Abstract method the Factory can supply a new Food type and leave Cows alone.
The Client always refers to the Food SuperType and the Factory will sort out the Concrete object to return.
The client works directly with the Abstract Factory and the Factory will create Products based on the Factory type.
Have ago and Play God.
namespace Martins.Abstract.Factory.WorldCreator
{
//AbstractFactory
abstract class PlanetsFactory
{
//Factory Methods
public abstract InteligentLife CreateInteligentLife();
public abstract Food CreateFood();
}
//ConcreteFactory implements PlanetsFactory
class EarthFactory : PlanetsFactory
{
public override InteligentLife CreateInteligentLife()
{
return new Human();
}
public override Food CreateFood()
{
return new Cow();
}
}
//ConcreteFactory implements PlanetsFactory
class MarsFactory : PlanetsFactory
{
public override InteligentLife CreateInteligentLife()
{
return new Martian();
}
public override Food CreateFood()
{
return new GreenSlime();
}
}
//AbstractProductA
abstract class Food
{
}
//AbstractProductB
abstract class InteligentLife
{
public abstract void Eat(Food h);
}
//ConcreteProductA1
class Cow : Food
{
}
//ConcreteProductB1
class Human : InteligentLife
{
public override void Eat(Food h)
{
//Humans Eat Cows
Console.WriteLine(this.GetType().Name + "s eat " + h.GetType().Name + "s");
}
}
//ConcreteProductA1
class GreenSlime : Food
{
}
//ConcreteProductB1
class Martian : InteligentLife
{
public override void Eat(Food h)
{
//Martian's Eat Slime
Console.WriteLine(this.GetType().Name + "s eat " + h.GetType().Name + "s");
}
}
//The Client works with Abstract Super Types
//Polymorphism kicks in to resolve the concrete implemented instance.
//Client
class PlanetEarth
{
private Food food;
private InteligentLife life;
public PlanetEarth(PlanetsFactory factory)
{
life = factory.CreateInteligentLife();
food = factory.CreateFood();
}
public void WhatsToEat()
{
life.Eat(food);
}
}
//The Client works with Abstract Super Types
//Polymorphism kicks in to resolve the concrete implemented instance.
//Client
class PlanetMars
{
private Food food;
private InteligentLife life;
public PlanetMars(PlanetsFactory factory)
{
life = factory.CreateInteligentLife();
food = factory.CreateFood();
}
public void WhatsToEat()
{
life.Eat(food);
}
}
//Test application that uses both Factories to create Earth and Mars
class MainApp
{
public static void Main()
{
//Create Earth and let the Earth factory create the InteligentLife and Food Products.
PlanetsFactory world1 = new EarthFactory();
PlanetEarth planet1 = new PlanetEarth(world1);
planet1.WhatsToEat();
//Create Mars and let the Mars factory create the InteligentLife and Food Products.
PlanetsFactory world2 = new MarsFactory();
PlanetMars planet2 = new PlanetMars(world2);
planet2.WhatsToEat();
Console.WriteLine("\r\nPlanets Factory Rest for 7 days");
}
}
}

No comments:
Post a Comment