Design Pattern ->Abstract Factory
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
Design Pattern ->Abstract Factory的更多相关文章
- LN : Eden Polymorphic And OOP Design Pattern Abstract Factory
Appreciation to our TA, +7, who designed this task. Client.cpp #include <iostream> #include &l ...
- [design pattern](5) Factory Method
前言 在前面一章博主介绍了简单工厂模式(Simple Factory),接着上面的章节,今天博主就来介绍下工厂方法模式(Factory Method). 思考题 首先,让我们来思考下面的问题: 在上一 ...
- 简单工厂设计模式(Simple Factory Design Pattern)
[引言]最近在Youtub上面看到一个讲解.net设计模式的视频,其中作者的一个理解让我印象很深刻:所谓的设计模式其实就是运用面向对象编程的思想来解决平时代码中的紧耦合,低扩展的问题.另外一点比较有见 ...
- [转]Design Pattern Interview Questions - Part 1
Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...
- Design Pattern ——Factory Method&Abstract Factory
今天开始复习设计模式.设计模式相关的资料有很多,概念性的东西就画个图就可以了.把关注点放在例子上,设计模式还是要使用中才有感受. 从Factory Method&Abstract Factor ...
- 大白话抽象工厂模式(Abstract Factory Pattern)
实例分析 大白话工厂方法模式(Factory Method)一文中,我们讲解了日产4S店工厂规模的扩大,创建了针对不同车型的工厂,减少了工厂的工作内容,提高了效率.下面我们继续以4S店的故事讨论抽象工 ...
- Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...
- [Design Pattern] Factory Pattern 简单案例
Factory Pattern , 即工厂模式,用于创建对象的场景,属于创建类的设计模式 . 下面是一个工厂模式案例. Shape 作为接口, Circle, Square, Rectangle 作为 ...
- design pattern factory method #Reprinted#
引入人.工厂.和斧子的问题: (1),原始社会时,劳动社会基本没有分工,需要斧子的人(调用者)只好自己去磨一把斧子,每个人拥有自己的斧子,如果把大家的石斧改为铁斧,需要每个人都要学会磨铁斧的本领,工作 ...
随机推荐
- Java类成员变量、普通成员变量、初始化块、构造方法的初始化和执行顺序
结论:执行的大致顺序如下, (1) 在一个不存在继承的类中:初始化static变量,执行static初始化块-->初始化普通成员变量(如果有赋值语句),执行普通初始化块-->构造方法 (2 ...
- C语言ctype.h字符函数和字符串
ctype.h存的是与字符相关的函数: 这些函数虽然不能处理整个字符串,但是可以处理字符串中的字符: ToUpper()函数,利用toupper()函数处理字符串中的每个字符,转换成大写: Punct ...
- day_08 文件操作
常用模式解释 open表示打开一个文件 f 变量, 操控XXX.txt文件的句柄 r:只读 w:只写 a:追加写入 b:byte,这种模式下,encoding不能用utf-8字符集 1. 文件的基本操 ...
- PHP任意文件上传漏洞(CVE-2015-2348)
安全研究人员今天发布了一个中危漏洞——PHP任意文件上传漏洞(CVE-2015-2348). 在上传文件的时候只判断文件名是合法的文件名就断定这个文件不是恶意文件,这确实会导致其他安全问题.并且在这种 ...
- environment与@ConfigurationProperties的关系 加载过程分析
environment是在printBanner之前就初始化好了, 更在context创建之前, 已经加载application-xxxx.properties, System.properties, ...
- 19. Remove Nth Node From End of List(C++,Python)
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- C++的友元函数
友元函数不是成员函数,是类外函数所以声明放在类的私有段或公有段且没有区别.但是它可以访问类中的私有成员.友元的作用在于提高程序的运行效率,但是,它破坏了类的封装性和隐藏性,使得非成员函数可以访问类的私 ...
- 关于火狐不响应event.keyCode解决方法
<body onkeydown="enterkey(event)"> <!-- 点击键盘即触发 enterkey函数 --> function enterk ...
- LeetCode 222.完全二叉树的节点个数(C++)
给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底 ...
- stm32串口学习(一)
串口在工作中经常用到,今天我们从零开始学习stm32的串口编程(利用库函数). 先从最简单的情况开始,假设我们要实现的功能就是串口发送一个字节,不考虑接收,也不考虑中断. 那么要解决两个问题: 1 串 ...