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