转:设计模式---抽象工厂模式(简洁明了)
2014-10-09 13:27
218 查看
转自:http://blog.csdn.net/ipqxiang/article/details/1955677
为了方便引进抽象工厂模式,引进一个新概念:产品族(Product Family)。所谓产品族,是指位于不同产品等级结构,功能相关联的产品组成的家族。如图:
// Abstract Factory pattern -- Structural example
using System;
// "AbstractFactory"
abstract class AbstractFactory
// "ConcreteFactory1"
class ConcreteFactory1 : AbstractFactory
// "ConcreteFactory2"
class ConcreteFactory2 : AbstractFactory
// "AbstractProductA"
abstract class AbstractProductA
// "AbstractProductB"
abstract class AbstractProductB
// "ProductA1"
class ProductA1 : AbstractProductA
// "ProductB1"
class ProductB1 : AbstractProductB
// "ProductA2"
class ProductA2 : AbstractProductA
// "ProductB2"
class ProductB2 : AbstractProductB
// "Client" - the interaction environment of the products
class Environment
class ClientApp
// Abstract Factory pattern -- Real World example
using System;
// "AbstractFactory"
abstract class ContinentFactory
// "ConcreteFactory1"
class AfricaFactory : ContinentFactory
// "ConcreteFactory2"
class AmericaFactory : ContinentFactory
// "AbstractProductA"
abstract class Herbivore
// "AbstractProductB"
abstract class Carnivore
// "ProductA1"
class Wildebeest : Herbivore
// "ProductB1"
class Lion : Carnivore
// "ProductA2"
class Bison : Herbivore
// "ProductB2"
class Wolf : Carnivore
// "Client"
class AnimalWorld
class GameApp
{
public static void Main( string[] args )
{
// Create and run the Africa animal world
ContinentFactory africa = new AfricaFactory();
AnimalWorld world = new AnimalWorld( africa );
world.RunFoodChain();
// Create and run the America animal world
ContinentFactory america = new AmericaFactory();
world = new AnimalWorld( america );
world.RunFoodChain();
}
}
抽象工厂的另外一个例子:
如何设计抽象类工厂留作思考。
增加产品族:Abstract Factory很好的支持了"开放-封闭"原则。
增加新产品的等级结构:需要修改所有的工厂角色,没有很好支持"开放-封闭"原则。
综合起来,抽象工厂模式以一种倾斜的方式支持增加新的产品,它为新产品族的增加提供方便,而不能为新的产品等级结构的增加提供这样的方便。
参考文献:
阎宏,《Java与模式》,电子工业出版社
[美]James W. Cooper,《C#设计模式》,电子工业出版社
[美]Alan Shalloway James R. Trott,《Design Patterns Explained》,中国电力出版社
[美]Robert C. Martin,《敏捷软件开发-原则、模式与实践》,清华大学出版社
[美]Don Box, Chris Sells,《.NET本质论 第1卷:公共语言运行库》,中国电力出版社
一、 抽象工厂(Abstract Factory)模式
抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。为了方便引进抽象工厂模式,引进一个新概念:产品族(Product Family)。所谓产品族,是指位于不同产品等级结构,功能相关联的产品组成的家族。如图:
// Abstract Factory pattern -- Structural example
using System;
// "AbstractFactory"
abstract class AbstractFactory
// "ConcreteFactory1"
class ConcreteFactory1 : AbstractFactory
// "ConcreteFactory2"
class ConcreteFactory2 : AbstractFactory
// "AbstractProductA"
abstract class AbstractProductA
// "AbstractProductB"
abstract class AbstractProductB
// "ProductA1"
class ProductA1 : AbstractProductA
// "ProductB1"
class ProductB1 : AbstractProductB
// "ProductA2"
class ProductA2 : AbstractProductA
// "ProductB2"
class ProductB2 : AbstractProductB
// "Client" - the interaction environment of the products
class Environment
class ClientApp
// Abstract Factory pattern -- Real World example
using System;
// "AbstractFactory"
abstract class ContinentFactory
// "ConcreteFactory1"
class AfricaFactory : ContinentFactory
// "ConcreteFactory2"
class AmericaFactory : ContinentFactory
// "AbstractProductA"
abstract class Herbivore
// "AbstractProductB"
abstract class Carnivore
// "ProductA1"
class Wildebeest : Herbivore
// "ProductB1"
class Lion : Carnivore
// "ProductA2"
class Bison : Herbivore
// "ProductB2"
class Wolf : Carnivore
// "Client"
class AnimalWorld
class GameApp
{
public static void Main( string[] args )
{
// Create and run the Africa animal world
ContinentFactory africa = new AfricaFactory();
AnimalWorld world = new AnimalWorld( africa );
world.RunFoodChain();
// Create and run the America animal world
ContinentFactory america = new AmericaFactory();
world = new AnimalWorld( america );
world.RunFoodChain();
}
}
抽象工厂的另外一个例子:
如何设计抽象类工厂留作思考。
七、 "开放-封闭"原则
"开放-封闭"原则要求系统对扩展开放,对修改封闭。通过扩展达到增强其功能的目的。对于涉及到多个产品族与多个产品等级结构的系统,其功能增强包括两方面:增加产品族:Abstract Factory很好的支持了"开放-封闭"原则。
增加新产品的等级结构:需要修改所有的工厂角色,没有很好支持"开放-封闭"原则。
综合起来,抽象工厂模式以一种倾斜的方式支持增加新的产品,它为新产品族的增加提供方便,而不能为新的产品等级结构的增加提供这样的方便。
参考文献:
阎宏,《Java与模式》,电子工业出版社
[美]James W. Cooper,《C#设计模式》,电子工业出版社
[美]Alan Shalloway James R. Trott,《Design Patterns Explained》,中国电力出版社
[美]Robert C. Martin,《敏捷软件开发-原则、模式与实践》,清华大学出版社
[美]Don Box, Chris Sells,《.NET本质论 第1卷:公共语言运行库》,中国电力出版社
相关文章推荐
- 设计模式C#描述——抽象工厂模式
- .NET设计模式-抽象工厂模式(Abstract Factory)
- 设计模式C#描述——抽象工厂模式
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
- 设计模式——创建型模式之 Abstract Factory抽象工厂模式
- [导入]C#面向对象设计模式纵横谈(3):Abstract Factory 抽象工厂模式(创建型模式)
- 设计模式C#描述——抽象工厂模式
- Java设计模式圣经连载(03)-抽象工厂模式
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
- 我的设计模式之旅(3)――抽象工厂模式AbstractFactory
- 设计模式实践(抽象工厂模式应用)—通信录的优化过程
- 设计模式之抽象工厂模式
- AspectJ实现设计模式(四)——抽象工厂模式
- .NET设计模式:抽象工厂模式(Abstract Factory)
- [导入]C#面向对象设计模式纵横谈(4):Abstract Factory 抽象工厂模式(创建型模式)
- 设计模式笔记-抽象工厂模式
- .Net设计模式之抽象工厂模式(Abstract Factory)
- .NET设计模式(3):抽象工厂模式(Abstract Factory)(转)
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
- 设计模式(3)-抽象工厂模式(Abstract Factory)