Design Patterns (.NET) Part-2

  1. Please read the first article from this link. It will help to catch the theory easily. https://www.dhirubhai.net/pulse/design-patterns-net-foysal-ahmed-sifat
  2. Abstract Factory Method

According to Gang of Four Definition: “The Abstract Factory Design Pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes“. It can be called the Factory of factories. Abstract Factory is a super factory that creates other factories.?

UML Diagram:

Abstract Factory Method Diagram

Implementational Example:

using System

namespace DoFactory.GangOfFour.Abstract.Structural

{

????/// MainApp startup class for Structural

????/// Abstract Factory Design Pattern.

????class MainApp

????{

????????/// Entry point into the console application.

????????public static void Main()

????????{

????????????// Abstract factory #1

????????????AbstractFactory factory1 = new ConcreteFactory1();

????????????Client client1 = new Client(factory1);

????????????client1.Run();

????????????// Abstract factory #2

????????????AbstractFactory factory2 = new ConcreteFactory2();

????????????Client client2 = new Client(factory2);

????????????client2.Run();

????????????// Wait for user input

????????????Console.ReadKey();

????????}

????}




????/// The 'AbstractFactory' abstract class

????abstract class AbstractFactory

????{

????????public abstract AbstractProductA CreateProductA();

????????public abstract AbstractProductB CreateProductB();

????}

????/// The 'ConcreteFactory1' class

????class ConcreteFactory1 : AbstractFactory

????{

????????public override AbstractProductA CreateProductA()

????????{

????????????return new ProductA1();

????????}

????????public override AbstractProductB CreateProductB()

????????{

????????????return new ProductB1();

????????}

????}




????/// The 'ConcreteFactory2' class

????class ConcreteFactory2 : AbstractFactory

????{

????????public override AbstractProductA CreateProductA()

????????{

????????????return new ProductA2();

????????}

????????public override AbstractProductB CreateProductB()

????????{

????????????return new ProductB2();

????????}

????}

????/// The 'AbstractProductA' abstract class

????abstract class AbstractProductA

????{

????}

????/// The 'AbstractProductB' abstract class

????abstract class AbstractProductB

????{

????????public abstract void Interact(AbstractProductA a);

????}

????/// The 'ProductA1' class

????class ProductA1 : AbstractProductA

????{

????}

????/// The 'ProductB1' class

????class ProductB1 : AbstractProductB

????{

????????public override void Interact(AbstractProductA a)

????????{

????????????Console.WriteLine(this.GetType().Name +

??????????????" interacts with " + a.GetType().Name);

????????}

????}

????/// The 'ProductA2' class

????class ProductA2 : AbstractProductA

????{

????}

????/// The 'ProductB2' class

????class ProductB2 : AbstractProductB

????{

????????public override void Interact(AbstractProductA a)

????????{

????????????Console.WriteLine(this.GetType().Name +

??????????????" interacts with " + a.GetType().Name);

????????}

????}

????/// The 'Client' class. Interaction environment for the products.

????class Client

????{

????????private AbstractProductA _abstractProductA;

????????private AbstractProductB _abstractProductB;




????????// Constructor




????????public Client(AbstractFactory factory)

????????{

????????????_abstractProductB = factory.CreateProductB();

????????????_abstractProductA = factory.CreateProductA();

????????}

????????public void Run()

????????{

????????????_abstractProductB.Interact(_abstractProductA);

????????}

????}

};        

The classes and objects participating in this pattern include:

  1. AbstractFactory? (ContinentFactory)
  2. declares an interface for operations that create abstract products
  3. ConcreteFactory ? (AfricaFactory, AmericaFactory)
  4. implements the operations to create concrete product objects
  5. AbstractProduct ? (Herbivore, Carnivore)
  6. declares an interface for a type of product object
  7. Product? (Wildebeest, Lion, Bison, Wolf)
  8. defines a product object to be created by the corresponding concrete factory
  9. implements the AbstractProduct interface
  10. Client? (AnimalWorld)
  11. uses interfaces declared by AbstractFactory and AbstractProduct classes


Sample:?

using System

namespace DoFactory.GangOfFour.Abstract.RealWorld

{

????/// MainApp startup class for Real-World

????/// Abstract Factory Design Pattern.

????class MainApp

????{

????????/// Entry point into console application.

????????public static void Main()

????????{




????????????// Create and run the African animal world

????????????ContinentFactory africa = new AfricaFactory();

????????????AnimalWorld world = new AnimalWorld(africa);

????????????world.RunFoodChain();

????????????// Create and run the American animal world




????????????ContinentFactory america = new AmericaFactory();

????????????world = new AnimalWorld(america);

????????????world.RunFoodChain();

????????????// Wait for user input

????????????Console.ReadKey();

????????}

????}

????/// The 'AbstractFactory' abstract class

????abstract class ContinentFactory

????{

????????public abstract Herbivore CreateHerbivore();

????????public abstract Carnivore CreateCarnivore();

????}







????/// The 'ConcreteFactory1' class

????class AfricaFactory : ContinentFactory

????{

????????public override Herbivore CreateHerbivore()

????????{

????????????return new Wildebeest();

????????}

????????public override Carnivore CreateCarnivore()

????????{

????????????return new Lion();

????????}

????}

????/// The 'ConcreteFactory2' class

????class AmericaFactory : ContinentFactory

????{

????????public override Herbivore CreateHerbivore()

????????{

????????????return new Bison();

????????}

????????public override Carnivore CreateCarnivore()

????????{

????????????return new Wolf();

????????} }




????/// The 'AbstractProductA' abstract class

????abstract class Herbivore

????{

????}

????/// The 'AbstractProductB' abstract class

????abstract class Carnivore

????{

????????public abstract void Eat(Herbivore h);

????}

????/// The 'ProductA1' class

????class Wildebeest : Herbivore

????{

????}

????/// The 'ProductB1' class

????????class Lion : Carnivore

????{

????????public override void Eat(Herbivore h)

????????{

????????????// Eat Wildebeest

????????????Console.WriteLine(this.GetType().Name +

??????????????" eats " + h.GetType().Name);

????????}

????}

????/// The 'ProductA2' class

??????class Bison : Herbivore

????{

????}

????/// The 'ProductB2' class

???????class Wolf: Carnivore

????{

????????public override void Eat(Herbivore h)

????????{

????????????// Eat Bison

????????????Console.WriteLine(this.GetType().Name +

??????????????" eats " + h.GetType().Name);

????????}

????}

????/// The 'Client' class?

????????class AnimalWorld

????{

????????private Herbivore _herbivore;

????????private Carnivore _carnivore;




????????// Constructor

????????public AnimalWorld(ContinentFactory factory)

????????{

????????????_carnivore = factory.CreateCarnivore();

????????????_herbivore = factory.CreateHerbivore();

????????}




????????public void RunFoodChain()

????????{

????????????_carnivore.Eat(_herbivore);

????????}

????}

};        

要查看或添加评论,请登录

Foysal Ahamed Sifat的更多文章

社区洞察

其他会员也浏览了