1. 定义
抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
2. UML 类图
3. 结构代码
该结构代码演示了创建并行对象层次结构的抽象工厂模式。对象创建是抽象的,也不需要硬编码的类名称在客户机代码中。
// Abstract Factory pattern -- Structural example |
using System;
namespace DoFactory.GangOfFour.Abstract.Structural { ///<summary> /// MainApp startup class for Structural /// Abstract Factory Design Pattern. ///</summary> classMainApp { ///<summary> /// Entry point into console application. ///</summary> publicstatic 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(); } }
///<summary> /// The 'AbstractFactory' abstract class ///</summary> abstractclass AbstractFactory { publicabstract AbstractProductA CreateProductA(); publicabstract AbstractProductB CreateProductB(); }
///<summary> /// The 'ConcreteFactory1' class ///</summary> classConcreteFactory1 : AbstractFactory { publicoverride AbstractProductA CreateProductA() { returnnew ProductA1(); } publicoverride AbstractProductB CreateProductB() { returnnew ProductB1(); } }
///<summary> /// The 'ConcreteFactory2' class ///</summary> classConcreteFactory2 : AbstractFactory { publicoverride AbstractProductA CreateProductA() { returnnew ProductA2(); } publicoverride AbstractProductB CreateProductB() { returnnew ProductB2(); } }
///<summary> /// The 'AbstractProductA' abstract class ///</summary> abstractclass AbstractProductA { }
///<summary> /// The 'AbstractProductB' abstract class ///</summary> abstractclass AbstractProductB { publicabstract void Interact(AbstractProductA a); }
///<summary> /// The 'ProductA1' class ///</summary> classProductA1 : AbstractProductA { }
///<summary> /// The 'ProductB1' class ///</summary> classProductB1 : AbstractProductB { publicoverride void Interact(AbstractProductA a) { Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name); } }
///<summary> /// The 'ProductA2' class ///</summary> classProductA2 : AbstractProductA { }
///<summary> /// The 'ProductB2' class ///</summary> classProductB2 : AbstractProductB { publicoverride void Interact(AbstractProductA a) { Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name); } }
///<summary> /// The 'Client' class. Interaction environment for the products. ///</summary> classClient { privateAbstractProductA _abstractProductA; privateAbstractProductB _abstractProductB;
// Constructor public Client(AbstractFactory factory) { _abstractProductB = factory.CreateProductB(); _abstractProductA = factory.CreateProductA(); }
publicvoid Run() { _abstractProductB.Interact(_abstractProductA); } } } |
ProductB1 interacts with ProductA1 |
4. 实例代码
该实例代码演示了在电脑游戏中使用不同的工厂来创建不同的动物世界
// Abstract Factory pattern -- Real World example |
using System;
namespace DoFactory.GangOfFour.Abstract.RealWorld { ///<summary> /// MainApp startup class for Real-World /// Abstract Factory Design Pattern. ///</summary> classMainApp { ///<summary> /// Entry point into console application. ///</summary> publicstatic 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(); } }
///<summary> /// The 'AbstractFactory' abstract class ///</summary> abstractclass ContinentFactory { publicabstract Herbivore CreateHerbivore(); publicabstract Carnivore CreateCarnivore(); }
///<summary> /// The 'ConcreteFactory1' class ///</summary> classAfricaFactory : ContinentFactory { publicoverride Herbivore CreateHerbivore() { returnnew Wildebeest(); } publicoverride Carnivore CreateCarnivore() { returnnew Lion(); } }
///<summary> /// The 'ConcreteFactory2' class ///</summary> classAmericaFactory : ContinentFactory { publicoverride Herbivore CreateHerbivore() { returnnew Bison(); } publicoverride Carnivore CreateCarnivore() { returnnew Wolf(); } }
///<summary> /// The 'AbstractProductA' abstract class ///</summary> abstractclass Herbivore { }
///<summary> /// The 'AbstractProductB' abstract class ///</summary> abstractclass Carnivore { publicabstract void Eat(Herbivore h); }
///<summary> /// The 'ProductA1' class ///</summary> classWildebeest : Herbivore { }
///<summary> /// The 'ProductB1' class ///</summary> classLion : Carnivore { publicoverride void Eat(Herbivore h) { // Eat Wildebeest Console.WriteLine(this.GetType().Name + " eats " + h.GetType().Name); } }
///<summary> /// The 'ProductA2' class ///</summary> classBison : Herbivore { }
///<summary> /// The 'ProductB2' class ///</summary> classWolf : Carnivore { publicoverride void Eat(Herbivore h) { // Eat Bison Console.WriteLine(this.GetType().Name + " eats " + h.GetType().Name); } }
///<summary> /// The 'Client' class ///</summary> classAnimalWorld { privateHerbivore _herbivore; privateCarnivore _carnivore;
// Constructor public AnimalWorld(ContinentFactory factory) { _carnivore = factory.CreateCarnivore(); _herbivore = factory.CreateHerbivore(); }
publicvoid RunFoodChain() { _carnivore.Eat(_herbivore); } } } |
Lion eats Wildebeest |