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的更多相关文章

  1. LN : Eden Polymorphic And OOP Design Pattern Abstract Factory

    Appreciation to our TA, +7, who designed this task. Client.cpp #include <iostream> #include &l ...

  2. [design pattern](5) Factory Method

    前言 在前面一章博主介绍了简单工厂模式(Simple Factory),接着上面的章节,今天博主就来介绍下工厂方法模式(Factory Method). 思考题 首先,让我们来思考下面的问题: 在上一 ...

  3. 简单工厂设计模式(Simple Factory Design Pattern)

    [引言]最近在Youtub上面看到一个讲解.net设计模式的视频,其中作者的一个理解让我印象很深刻:所谓的设计模式其实就是运用面向对象编程的思想来解决平时代码中的紧耦合,低扩展的问题.另外一点比较有见 ...

  4. [转]Design Pattern Interview Questions - Part 1

    Factory, Abstract factory, prototype pattern (B) What are design patterns? (A) Can you explain facto ...

  5. Design Pattern ——Factory Method&Abstract Factory

    今天开始复习设计模式.设计模式相关的资料有很多,概念性的东西就画个图就可以了.把关注点放在例子上,设计模式还是要使用中才有感受. 从Factory Method&Abstract Factor ...

  6. 大白话抽象工厂模式(Abstract Factory Pattern)

    实例分析 大白话工厂方法模式(Factory Method)一文中,我们讲解了日产4S店工厂规模的扩大,创建了针对不同车型的工厂,减少了工厂的工作内容,提高了效率.下面我们继续以4S店的故事讨论抽象工 ...

  7. Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...

  8. [Design Pattern] Factory Pattern 简单案例

    Factory Pattern , 即工厂模式,用于创建对象的场景,属于创建类的设计模式 . 下面是一个工厂模式案例. Shape 作为接口, Circle, Square, Rectangle 作为 ...

  9. design pattern factory method #Reprinted#

    引入人.工厂.和斧子的问题: (1),原始社会时,劳动社会基本没有分工,需要斧子的人(调用者)只好自己去磨一把斧子,每个人拥有自己的斧子,如果把大家的石斧改为铁斧,需要每个人都要学会磨铁斧的本领,工作 ...

随机推荐

  1. Orcale常用函数

    1.ascii 作用: 返回指定的字符对应的十进制数 select ascii('A') ,ascii('a'),ascii(' ') from dual; 2.chr 作用:给出整数,返回对应的字符 ...

  2. Oracle表的优化一点见解

    Oracle优化的几个简单步骤 数据库优化的讨论可以说是一个永恒的主题.资深的Oracle优化人员通常会要求提出性能问题的人对数据库做一个statspack,贴出数据库配置等等.还有的人认为要抓出执行 ...

  3. Julia体验 语言基础

    以前听说过Julia,不过那时候官网还处于时不时宕机状态,最近Julia发布了1.0 released版本到处都是它的资讯,官网良心自带简体中文,趁着热度我也来试试,顺便聊记一二. 关于Julia J ...

  4. 牛客寒假算法基础集训营4 F Applese 的QQ群

    链接:https://ac.nowcoder.com/acm/contest/330/F来源:牛客网 Applese 有一个QQ群.在这个群中,大家互相请教问题.如 b 向 a 请教过问题,就把 a ...

  5. linux下mysql的安装部署

    ---恢复内容开始--- 注意这一切都是root用户下进行的 su root  * 一.查看之前是否安装过:yum list installed mysql* 二.查看是否有安装包:yum list ...

  6. centos yum命令找不到包

    https://blog.csdn.net/joe_le/article/details/80107832 yum  install epel-release  //扩展包更新包 yum  updat ...

  7. mysql不支持emoji表情的问题的解决方法

    最近财神圈项目集成微信登录功能的过程中,当保存用户有昵称含有表情符号时后台服务抛出异常,原来是数据库默认字符集不支持emoji表情字符.找到问题的原因后,因为之前也没有遇到过这样的问题,也没思路,迅速 ...

  8. html 一些坑。。。

    margin-left 和 margin-right 才能够使用 auto top 和 bottom 不可以 在网页设计中...通常给img 父容器 一个 宽度...同时 ,指定 img width= ...

  9. css雪碧图制作

    使用css背景合并工具cssSprite 工具下载链接: http://download.csdn.net/download/wx247919365/8741243 1.选择文件 2.生成雪碧图 3. ...

  10. 3.Servlet(二)

    1.Servlet应用开发接口 对Servlet应用开发接口及功能的掌握,决定了是否能做好Servlet开发工作. GenericServlet抽象类 HttpServlet抽象类 2.Servlet ...