一 概念
- 抽象工厂模式,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
二 包含的角色
- 抽象工厂:它里面应该包含所有的产品创建的抽象方法
- 具体工厂:具体工厂,创建具有特定实现的产品对象
- 抽象产品:他们都有可能有两种不同的实现
- 具体产品
三 优势
- 抽象工厂模式是对工厂方法模式的改进。用于处理产品不只有一类的情况(工厂方法模式下,产品只有User这一类,而抽象工厂模式下,产品包括User和Department两类)。
四 抽象工厂模式的缺点
-抽象工厂模式的缺点来自增加功能。
五 C++代码实现
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
//数据库表项:User 与工厂模式无关
class User
{
public:
void SetId(const int id)
{
this->_id = id;
}
int GetId()
{
return this->_id;
}
void SetName(const string name)
{
this->_name = name;
}
string GetName()
{
return this->_name;
}
private:
int _id;
string _name;
};
//数据库表项 Department 与工厂模式无关
class Department
{
public:
void SetId(const int id)
{
this->_id = id;
}
int GetId()
{
return this->_id;
}
void SetName(const string name)
{
this->_deptName = name;
}
string GetName()
{
return this->_deptName;
}
private:
int _id;
string _deptName;
};
//抽象产品A: IUser
class IUser
{
public:
virtual void Insert(User user) = 0;
virtual User* GetUser(int id) = 0;
};
//具体产品A1 SqlserverUser
class SqlserverUser : public IUser
{
public:
void Insert(User user)
{
cout << "在SQL server中给User表增加一条记录" << endl;
}
User* GetUser(int id)
{
cout << "在SQL server中根据ID得到User表一条记录" << endl;
return NULL;
}
};
//具体产品A2 AccessUser
class AccessUser : public IUser
{
public:
void Insert(User user)
{
cout << "在Access 中给User表增加一条记录" << endl;
}
User* GetUser(int id)
{
cout << "在Access 中根据ID得到User表一条记录" << endl;
return NULL;
}
};
//抽象产品B: IDepartment
class IDepartment
{
public:
virtual void Insert(Department department) = 0;
virtual Department* GetUser(int id) = 0;
};
//具体产品B1 SqlserverDepartment
class SqlserverDepartment : public IDepartment
{
public:
void Insert(Department department)
{
cout << "在SQL server中给Department表增加一条记录" << endl;
}
Department* GetUser(int id)
{
cout << "在SQL server中根据ID得到Department表一条记录" << endl;
return NULL;
}
};
//具体产品B2 AccessDepartment
class AccessDepartment : public IDepartment
{
public:
void Insert(Department department)
{
cout << "在Access 中给Department表增加一条记录" << endl;
}
Department* GetUser(int id)
{
cout << "在Access 中根据ID得到Department表一条记录" << endl;
return NULL;
}
};
//抽象工厂
//抽象工厂接口,它里面应该包含所有的产品创建的抽象方法
class IFactory
{
public:
virtual IUser* createUser() = 0;
virtual IDepartment* createDepartment() = 0;
};
//具体工厂1,创建具有特定实现的产品对象
class SqlServerFactory : public IFactory
{
public:
IUser* createUser()
{
IUser* puser;
puser = new SqlserverUser();
return puser;
}
IDepartment* createDepartment()
{
IDepartment* pdepartment;
pdepartment = new SqlserverDepartment();
return pdepartment;
}
};
class AccessFactory : public IFactory
{
public:
IUser* createUser()
{
IUser* puser;
puser = new AccessUser();
return puser;
}
IDepartment* createDepartment()
{
IDepartment* pdepartment;
pdepartment = new AccessDepartment();
return pdepartment;
}
};
int main()
{
User user;
Department department;
//只需要确定实例化哪一个数据库访问对象给factory
IFactory* factory = new AccessFactory;
//则此时已与具体的数据库访问解除了依赖
IUser* iu = factory->createUser();
iu->Insert(user);
iu->GetUser(1);
//则此时已与具体的数据库访问解除了依赖
IDepartment* id = factory->createDepartment();
id->Insert(department);
id->GetUser(1);
return 0;
}
参考资料:
1 《大话设计模式C++实现-第15章-抽象工厂模式》 https://blog.csdn.net/xiqingnian/article/details/41181995
2 《大话设计模式》 程杰 著