《大话设计模式》c++实现 抽象工厂模式
为了更清晰地理解工厂方法模式,需要先引入两个概念:
- 产品等级结构 :产品等级结构即产品的继承结构,如一个抽象类是电视机,其子类有海尔电视机、海信电视机、TCL电视机,则抽象电视机与具体品牌的电视机之间构成了一个产品等级结构,抽象电视机是父类,而具体品牌的电视机是其子类。
- 产品族 :在抽象工厂模式中,产品族是指由同一个工厂生产的,位于不同产品等级结构中的一组产品,如海尔电器工厂生产的海尔电视机、海尔电冰箱,海尔电视机位于电视机产品等级结构中,海尔电冰箱位于电冰箱产品等级结构中。
抽象工厂模式包含如下角色:
- AbstractFactory:抽象工厂
- ConcreteFactory:具体工厂
- AbstractProduct:抽象产品
- Product:具体产品

在上例中,产品族包括两个:1和2,也就是Sqlserver数据库和Access数据库。
每个产品族里面又包含两类产品:A和B,也就是User表和Department表。
而每个产品族中的产品要一起使用,就是说产品族1中的两类产品A和B要一起使用,也就是说在SqlServer数据库中SqlServerUser表和SqlServerDepartment表要一起使用,Access数据库同理。
工厂角度:
SqlFactory:
User:
//具体产品A1 SqlProductA
Department:
//具体产品B1 SqlProductB
AccessFactory:
User:
//具体产品A2 AccessProductA
Department:
//具体产品B2 AccessProductB
产品角度:
AbstractProductA(user 表):
//具体产品A1 SqlProductA
//具体产品A2 AccessProductA
AbstractProductB(Department表)
//具体产品B1 SqlProductA
//具体产品B2 AccessProductA
#include<string>
#include<iostream> //数据库表项:User 与工厂模式无关
class User
{
private:
int id;
std::string name;
public:
int getID()
{
return id;
}
std::string getName()
{
return name;
}
void setID(int ID)
{
this->id = ID;
}
void setName(std::string NAME)
{
this->name = NAME;
}
}; //数据库表项:Department 与工厂模式无关
class Department
{
private:
int id;
std::string name;
public:
int getID()
{
return id;
}
std::string getName()
{
return name;
}
void setID(int ID)
{
this->id = ID;
}
void setName(std::string NAME)
{
this->name = NAME;
}
}; //抽象产品A:IUser
class AbstractProductA{
public:
virtual void Insert(User user) = ;
virtual User* GetUser(int id) = ;
}; //具体产品A1 SqlProductA
class SqlProductA :public AbstractProductA{
public:
void Insert(User user){
std::cout << "在SQL Server中给User表增加了一条记录" << std::endl;
}
User* GetUser(int id){
std::cout << "在SQL Server中得到id为:"<<id<<" User表一条记录" << std::endl;
return NULL;
}
};
//具体产品A2 AccessProductA
class AccessProductA :public AbstractProductA{
public:
void Insert(User user){
std::cout << "在Access中给User表增加了一条记录" << std::endl;
}
User* GetUser(int id){
std::cout << "在Access中得到id为:" << id << " User表一条记录" << std::endl;
return NULL;
}
}; //抽象产品B:IDepartment
class AbstractProductB{
public:
virtual void Insert(Department department) = ;
virtual Department* GetDepartment(int id) = ;
}; //具体产品B1 SqlProductB
class SqlProductB :public AbstractProductB{
public:
void Insert(Department department){
std::cout << "在SQL Server中给department表增加了一条记录" << std::endl;
}
Department* GetDepartment(int id){
std::cout << "在SQL Server中得到id为:" << id << " Department表一条记录" << std::endl;
return NULL;
}
}; //具体产品B2 AccessProductB
class AccessProductB :public AbstractProductB{
public:
void Insert(Department department){
std::cout << "在Access中给department表增加了一条记录" << std::endl;
}
Department* GetDepartment(int id){
std::cout << "在Access中得到id为:" << id << " Department表一条记录" << std::endl;
return NULL;
}
}; class AbstractFactory{
public:
virtual AbstractProductA* createProductA() = ;
virtual AbstractProductB* createProductB() = ;
}; //具体工厂1:SqlServerFactory
class SqlFactory :public AbstractFactory
{
public:
AbstractProductA* createProductA()
{
return new SqlProductA;
}
AbstractProductB* createProductB()
{
return new SqlProductB;
}
}; //具体工厂2:AccessFactory
class AccessFactory :public AbstractFactory{
public:
AbstractProductA* createProductA()
{
return new AccessProductA;
}
AbstractProductB* createProductB()
{
return new AccessProductB; }
}; void main()
{
//初始化数据库
User user;
Department department; //ConcreteFactory1
AbstractFactory* factory = NULL;
//factory = new SqlFactory;
factory = new AccessFactory;
//ProductA1
AbstractProductA* iu = NULL;
iu = factory->createProductA();
iu->Insert(user);
iu->GetUser(); //ProductB1
AbstractProductB* id = NULL;
id = factory->createProductB();
id->Insert(department);
id->GetDepartment(); if (factory != NULL)
{
delete factory;
factory = NULL;
}
if (iu != NULL)
{
delete iu;
iu = NULL;
}
if (id != NULL)
{
delete id;
id = NULL;
} system("pause"); }
https://design-patterns.readthedocs.io/zh_CN/latest/creational_patterns/abstract_factory.html
https://blog.csdn.net/xiqingnian/article/details/41181995
《大话设计模式》c++实现 抽象工厂模式的更多相关文章
- 大话设计模式C++版——抽象工厂模式
前面说过,简单工厂模式是最基础的一种设计模式,那以工厂命名的设计模式就是23种设计模式中最多的一种,他们一脉相承,一步一步进化而来,这里就是其中的最后一种——抽象工厂模式(Abstract Facto ...
- 大话设计模式Python实现- 抽象工厂模式
抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们的类 下面是一个抽象工厂的demo: #!/usr/bin/env pyth ...
- JAVA常用设计模式(一、抽象工厂模式)
抽象工厂模式 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最 ...
- 设计模式学习心得<抽象工厂模式 Abstract Factory>
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂.该超级工厂又称为其他工厂的工厂.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 在抽 ...
- C#设计模式系列:抽象工厂模式(AbstractFactory)
出自:http://www.cnblogs.com/libingql/archive/2012/12/09/2809754.html 1. 抽象工厂模式简介 1.1 定义 抽象工厂(Abstract ...
- [Python编程实战] 第一章 python的创建型设计模式1.1抽象工厂模式
注:关乎对象的创建方式的设计模式就是“创建型设计模式”(creational design pattern) 1.1 抽象工厂模式 “抽象工厂模式”(Abstract Factory Pattern) ...
- IOS设计模式浅析之抽象工厂模式(Abstract Factory)
概述 在前面两章中,分别介绍了简单工厂模式和工厂方法模式,我们知道简单工厂模式的优点是去除了客户端与具体产品的依赖,缺点是违反了“开放-关闭原则”:工厂方法模式克服了简单工厂模式的缺点,将产品的创建工 ...
- 【java设计模式】-03抽象工厂模式
抽象工厂 简述 抽象工厂模式(Abstract Factory Pattern):提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类.在抽象工厂模式中,接口是负责创建一个相关对象的工厂 ...
- 一天一个设计模式——Abstract Factory抽象工厂模式
一.模式说明 前面学习了工厂方法(Factory Method)模式.在工厂方法模式中,在工厂方法模式中,父类决定如何生成实例,但并不决定所要生成的具体类,具体的处理交由子类来处理.这里学习的抽象工厂 ...
- 重学 Java 设计模式:实战抽象工厂模式
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获!
随机推荐
- [daily][CentOS][SELinux]用key免登陆不成功,原来是SElinux在搞事情
为了提高效率,一般情况下,会把公钥放到sshd主机的 $HOME/.ssh/authorized_keys 文件内. 把私钥放在client的 $HOME/.ssh/ 下. 然后就可以免密登录了.然而 ...
- airflow
基于airflow官方镜像制作自己的镜像,如给镜像安装pymongo FROM /common/air_grpc: MAINTAINER zhangchunyang@goldwind.com.cn U ...
- 转:eclipse maven build、maven install 等区别
原文地址:eclipse maven build.maven install 等区别
- Master-Worker集群计算demo
Task为要执行的任务实体类: package com.viewhigh.mdop.bi.test; /** * Created by zzq on 2017/5/11. */ public clas ...
- vuex的小demo
效果图: vue的app.vue <template> <div> <p>click {{count}} times,count is {{evenOrOdd}}& ...
- Java+Selenium如何解决空指针
1.问题描述:浏览器获取当期窗口值获取为空.
- 写一致性原理以及quorum机制
(1)consistency,one(primary shard),all(all shard),quorum(default)我们在发送任何一个增删改操作的时候,比如 PUT /index/type ...
- SpringBoot-整合lombok
添加lombok依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lom ...
- 第一节:Git下载和安装
1.下载并安装github客户端:https://desktop.github.com/ 2.进入自己的主页,新建一个项目:
- thinkphp无法安装提示修改mysql配置
在安装以thinkphp为框架的系统时数据库连接错误,提示修改sql-mode或sql_mode为NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION.那我们就顺着提示 ...