目录
基本概念
代码与实例
基本概念
抽象工厂模式(Abstract Factory),提供一个创建一系列相关或相互依赖对象的接口,从而无需指定他们具体的类。
抽象工厂的好处:
1. 易于交换产品系列,在一个应用中只需要在初始化的时候出现一次,这就使得改变一个应用的具体工厂变得非常容易,它只需要改变具体工厂即可使用不同的产品配置;
2. 让具体创建实例过程与客户端分离,客户端是通过它们的抽象接口操纵实例,产品的具体类名也被具体工厂分离,不会在客户端代码中出现
对应的UML图如下(此图来源于大话设计模式)
代码与实例
程序运行截图如下:
源码如下:
Head.h
#ifndef HEAD_H#define HEAD_H#include <iostream>#include <string>using namespace std;typedef string Department;typedef string User;//IUser接口,用于客户端访问,解除与具体数据库访问耦合class IUser{public:virtual void insert(User *user) {}virtual User getUser(int id){ return ""; }virtual ~IUser(){}};//SqlserverUser类,用于访问SQL Server的Userclass SqlserverUser : public IUser{public:void insert(User *user);User getUser(int id);~SqlserverUser();};//AccessUser类,用于访问Access的Userclass AccssUser : public IUser{public:void insert(User *user);User getUser(int id);~AccssUser();};//IDepartment接口,用于客户端访问,解除与具体数据库访问的耦合class IDepartment{public:virtual void insert(Department *department){}virtual Department *getDepartment(int id){ return nullptr; }virtual ~IDepartment(){}};//SqlServerDepartment类,用于访问SQL Server的Departmentclass SqlServerDepartment : public IDepartment{public:void insert(Department *department);Department *getDepartment(int id);~SqlServerDepartment();};//AccessDepartment类,用于访问Access的Deparmentclass AccessDepartment : public IDepartment{public:void insert(Department *department);Department *getDepartment(int id);~AccessDepartment();};//IFactory接口,定义一个创建访问Department表对象的抽象的工厂接口class IFactory{public:virtual IUser *createUser() { return nullptr; }virtual IDepartment *createDepartment(){ return nullptr; }virtual ~IFactory(){}};//SqlserverFactory类,实现IFactory接口,实例化SqlserverUser和SqlserverDepartmentclass SqlserverFactory : public IFactory{public:IUser *createUser();IDepartment *createDepartment();~SqlserverFactory();};//AccessFactory类,实现IFactory接口,实例化AccessUser和AccessDepartmentclass AccessFactory : public IFactory{public:IUser *createUser();IDepartment *createDepartment();~AccessFactory();};#endif//HEAD_H
Head.cpp
#include "Head.h"void SqlServerDepartment::insert(Department *department){cout << "在 SQL Server 中给 Department 表增加一条记录!" << endl;}Department *SqlServerDepartment::getDepartment(int id){cout << "在 SQL Server 中根据 ID 得到 Department 表中的一条记录!" << endl;return nullptr;}SqlServerDepartment::~SqlServerDepartment(){cout << "SqlServerDepartment desconstruction ! " << endl;}void AccessDepartment::insert(Department *department){cout << "在 Access 中给 Department 表增加一条记录!" << endl;}Department *AccessDepartment::getDepartment(int id){cout << "在 Access 中根据 ID 得到 Department 表中的一条记录!" << endl;return nullptr;}AccessDepartment::~AccessDepartment(){cout << "AccessDepartment desconstruction ! " << endl;}void SqlserverUser::insert(User *user){cout << "在 SQL Server 中给 User 表增加一条记录!" << endl;}User SqlserverUser::getUser(int id){cout << "在 SQL Server 中根据ID得到 User 表一条记录!" << endl;return "";}SqlserverUser::~SqlserverUser(){cout << "SqlserverUser desconstruction ! " << endl;}void AccssUser::insert(User *user){cout << "在 Access 中给 User 表增加一条记录!" << endl;}User AccssUser::getUser(int id){cout << "在 Access 中根据ID得到 User 表一条记录!" << endl;return "";}AccssUser::~AccssUser(){cout << "AccssUser desconstruction ! " << endl;}IUser * SqlserverFactory::createUser(){return new SqlserverUser;}IDepartment * SqlserverFactory::createDepartment(){return new SqlServerDepartment;}SqlserverFactory::~SqlserverFactory(){cout << "SqlserverFactory desconstruction ! " << endl;}IUser * AccessFactory::createUser(){return new AccssUser;}IDepartment * AccessFactory::createDepartment(){return new AccessDepartment;}AccessFactory::~AccessFactory(){cout << "AccessFactory desconstruction ! " << endl;}
main.cpp
#include "Head.h"int main(int *argc, int *argv){User *user = new User();Department *department = new Department();IFactory *factory = new SqlserverFactory();//IFactory *factory = new AccessFactory();IUser *iuser = factory->createUser();iuser->insert(user);iuser->getUser(1);IDepartment *iDepartment = factory->createDepartment();iDepartment->insert(department);iDepartment->getDepartment(1);cout << "\n 开始析构了!" << endl;delete iDepartment;delete iuser;delete factory;delete user;delete department;cout << "\n------------ 华 丽 的 结 束 分 割 线 ------------" << endl;getchar();return 0;}