//抽象工厂模式 AbstrctFactory
#define __CRT_SECURE_NO_WARNINGS
/*
@作者:莫忘输赢
@时间:
2020/02/20 21:20
@版本:v1
@抽象工厂模式
@作用:
定义一个创建一系列相关或相互依赖接口,无需指定它们的具体类
用于交换产品系列 如 acesss sql server
产品具体类 被具体工厂实现分离
*/
#include<iostream>
#include<vector>
//#include<vld.h>
//用户抽象接口
class IUser
{
public:
virtual void GetUser() = 0;
virtual void InsertUser() = 0;
};
//部门抽象接口
class IDepartment
{
public:
virtual void GetDepartment() = 0;
virtual void InsertDepartment() = 0;
};
class IFactory
{
public:
virtual IUser* CreateUser() = 0;
virtual IDepartment* CreateDepartment() = 0;
};
//access 用户
class CAccessUser : public IUser
{
public:
virtual void GetUser()
{
std::cout << "access GetUser" << std::endl;
}
virtual void InsertUser()
{
std::cout << "access InsertUser" << std::endl;
}
};
//access部门
class CAccessDepartment : public IDepartment
{
public:
virtual void GetDepartment()
{
std::cout << "access GetDepartment" << std::endl;
}
virtual void InsertDepartment()
{
std::cout << "access InsertDepartment" << std::endl;
}
};
//access类厂
class CAccessIFactory : public IFactory
{
public:
virtual IUser* CreateUser()
{
return new CAccessUser();
}
virtual IDepartment* CreateDepartment()
{
return new CAccessDepartment();
}
};
//sql 用户
class CSqlUser : public IUser
{
public:
virtual void GetUser()
{
std::cout << "sql GetUser" << std::endl;
}
virtual void InsertUser()
{
std::cout << "sql InsertUser" << std::endl;
}
};
//sql部门
class CSqlDepartment : public IDepartment
{
public:
virtual void GetDepartment()
{
std::cout << "sql GetDepartment" << std::endl;
}
virtual void InsertDepartment()
{
std::cout << "sql InsertDepartment" << std::endl;
}
};
//sql类厂
class CSqlIFactory : public IFactory
{
public:
virtual IUser* CreateUser()
{
return new CSqlUser();
}
virtual IDepartment* CreateDepartment()
{
return new CSqlDepartment();
}
};
int main(int argc, char **argv)
{
IFactory* factory = new CSqlIFactory();
IUser* user = factory->CreateUser();
IDepartment *department = factory->CreateDepartment();
user->GetUser();
department->GetDepartment();
delete user;
delete department;
delete factory;
return 0;
}