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),原始社会时,劳动社会基本没有分工,需要斧子的人(调用者)只好自己去磨一把斧子,每个人拥有自己的斧子,如果把大家的石斧改为铁斧,需要每个人都要学会磨铁斧的本领,工作 ...
随机推荐
- 9.27下午考试(Nescafé 29杯模拟赛)
140pts(100+30+10)Rank3 前几天还考了一场,AK,没什么好总结的,所以就没写博客. 炸: T2,模拟退火突然不会了,写个状压dp,排序边的时候sort的N而不是M. 这个坑经常出! ...
- 高仿JDK动态代理 底层源码实现
动态代理实现思路 实现功能:通过Proxy.newProxyInstance返回代理对象 1.创建一个处理业务逻辑的接口,我们也和JDK一样,都使用InvocationHandler作为接口名,然后接 ...
- BA 认证
IIBA-CBAP https://www.iiba.org/standards-and-resources/core-standard/ PMI-PBA https://www.pmi.org/ce ...
- vue.js组件之j间的通讯三,通过单一事件来管理组件通讯
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- CSS趣味
谈一下小技巧: 1.先看一下问题,实现下图,只用于一个html元素有多少种实现方式? 假设我们的单标签是一个 div: <div></div> 定义如下通用CSS: div{ ...
- [转]cron语法
最近在搞whenever时看到可以用cron语法设置定时任务.所以研究了下cron 语法. every '0 0 27-31 * *'do command "echo 'you can us ...
- Luogu P5201 [USACO19JAN]Shortcut 最短路树???
最短路树...开眼界了...之前想也没想过.... 先跑出来1到每个点最短路,然后建树时要标记点的入度,否则会多连边...然后深搜时更新新答案就是 #include<cstdio> #in ...
- BestCoder Round #66 1002
GTW likes gt Accepts: 75 Submissions: 261 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 1 ...
- testlink数据库访问密码修改
testlink重启后数据库连接不上将会报错 错误:1045 - Access denied for user 'my_db '@'localhost' (using password: YES) 怎 ...
- Django易混淆问题
1.Django本身提供了runserver 为什么不能用来 部署 runserver 方法是调试 Django 时经常用到的运行方式,它使用 Django 自带的 WSGI Server 运行,主 ...