Layering & Contract Philosophy With additional indirection

Abstract Factory

 //The example code is as following:
class CAbstractProductA;
class CAbstractProductB;
// multiple families of products.
class CProductA1:public CAbstractProductA;
class CProductB1:public CAbstractProductB;
class CProductA2:public CAbstractProductA;
class CProductB2:public CAbstractProductB; class CAbstractFactory
{
public:
virtual CAbstractProductA* CreateProductA() = ;
virtual CAbstractProductA* CreateProductB() = ;
};
class CConreateFactory1:public CAbstractFactory; //indirection layer
{
public:
virtual CAbstractProductA* CreateProductA() { return new CProductA1; }
virtual CAbstractProductB* CreateProductB() { return new CProductB1; }
}
class CConreateFactory2:public CAbstractFactory;//indirection layer
{
public:
virtual CAbstractProductA* CreateProductA() { return new CProductA2; }
virtual CAbstractProductB* CreateProductB() { return new CProductB2; }
} class CClient
{
public:
CAbstractFactory *pFactory = NULL;
void main()
{
if ( the system is windows )
{
pFactory = new CConreateFactory1();
}
else
{
pFactory = new CConreateFactory2();
}
CAbstractProductA *pProductA = pFactory->CreateProductA();
CAbstractProductB *pProductB = pFactory->CreateProductB();
}
};

Applicability
Use the Abstract Factory pattern when:

  • A system should be independent of how its products are created, compose /decompose, and represented.
  • A system should be configured with one of multiple families of products.
  • A family of related product objects is designed to be used together, and you need to enforce this constraint.
  • You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations. Interface-Oriented programming, Open For Extension, Close For Change,These All is said to Users or Client. Users don’t care or never know what is changed, so called “Close For Change”.
  • You want to create a set of product without changing client code. Just create a new pointer to the object instantiated with class derived from base class AbstractClass.

Participants

  • AbstractFactory (WidgetFactory): declares an interface for operations that create abstract product objects.
  • ConcreteFactory (MotifWidgetFactory, PMWidgetFactory):implements the operations to create concrete product objects.
  • AbstractProduct (Window, ScrollBar):declares an interface for a type of product object.
  • ConcreteProduct (MotifWindow, MotifScrollBar): concrete / specific defines a product object to be created by the corresponding concrete factory,implements the AbstractProduct interface.
  • Client: uses only interfaces declared by AbstractFactory and AbstractProduct classes.

Collaborations

  • Normally a single instance of a ConcreteFactory class is created at run-time.This concrete factory creates product objects having a particular implementation. To create different product objects, clients should use a different concrete factory.The Client get the a pointer or reference from the Singleton object created with ConcreteFactory at run-time.
  • AbstractFactory defers creation of product objects to its ConcreteFactory subclass.

From:Design Patterns:Elements of Reusable Object-Oriented Software, by GoF

最新文章

  1. FPGA书籍
  2. 直接操作游戏对象C#游戏开发
  3. Codeforces Round #313 (Div. 2) C. Gerald's Hexagon
  4. NServiceBus-网关和多站点分布
  5. 在Linux上部署和操作Couchbase
  6. oracle 安装, oem 不能正常使用,em安装失败、找不到dbconsole服务
  7. Linux上使用Azure CLI来管理Azure
  8. java socket 的参数选项解读(转)
  9. 转:Jmeter--google plugin插件监控被测系统资源方法
  10. Masonry布局框架的使用
  11. SpringBoot使用Maven插件打包部署
  12. 小白学习Python之路---re模块学习和挑战练习
  13. Module(CP343-1)Advanced system error SDB generation Error in rule file or rule file not found
  14. SpringBoot获取配置文件的自定义参数
  15. Vue(九)小案例 - 百度搜索列表(跨域)
  16. php+C#.net混合开发
  17. springboot中使用druid和监控配置
  18. 关于设置服务器为https服务器
  19. Atitit.多媒体区----web视频格式的选择总结
  20. hadoop20---代理另一种方式

热门文章

  1. Python 之 装饰器
  2. leecode第三题(无重复字符的最长子串)
  3. crm web ui
  4. Dictionary 检测key是否存在
  5. Spring 配置RMI远程调用
  6. java程序员的从0到1:@Resource与@Autowired的比较
  7. Pandas基本功能详解
  8. classloader 学习
  9. my24_mysql索引-使用篇
  10. The 'gridview' module MUST be setup in your Yii configuration file.