抽象工厂模式(Abstract Factory):提供一个创建一系列相关或者相互依赖对象的接口,而无需指定他们具体的类。
优点:便于交换产品系统,由于具体工厂类,在一个应用中只需要初始化的时候出现一次,这就使得改变一个应用的具体工厂变得非常容易,只需要改变具体工厂即可以使用不同的产品配置。它让具体的创建实例过程与客户端分离,客户端是通过他们的抽象接口操作实例,产品的具体类名也被具体工厂实现分离,不会出现在客户端代码中。
#ifndef ABSTRACTFACTORY_H #define ABSTRACTFACTORY_H #include<iostream> #include<string> using namespace std; class IDepartment { int id; string depname; public: virtual void Insert(IDepartment * dp)=0; virtual IDepartment * GetDepartment(int id)=0; }; class SqlserverDepartment :public IDepartment { public: void Insert(IDepartment * dp) ; IDepartment * GetDepartment(int id) ; }; void SqlserverDepartment::Insert(IDepartment * dp) { cout << "在SQL Server 中给Department表增加一条记录。\n"; } IDepartment * SqlserverDepartment::GetDepartment(int id) { cout << "在SQL Server 中根据ID得到Department表的一条记录。\n"; return NULL; } class AccessDepartment :public IDepartment { public: void Insert(IDepartment * dp); IDepartment * GetDepartment(int id); }; void AccessDepartment::Insert(IDepartment * dp) { cout << "在Access 中给Department表增加一条记录。\n"; } IDepartment * AccessDepartment::GetDepartment(int id) { cout << "在Access 中根据ID得到Department表的一条记录。\n"; return NULL; } class IUser { public: virtual void Insert(IUser *) = 0; virtual IUser * GetUser(int id) = 0; }; class SqlserverUser :public IUser { public: void Insert(IUser *); IUser * GetUser(int id); }; void SqlserverUser::Insert(IUser * in) { cout << "在SQL Server中给User表增加一条记录。\n"; } IUser * SqlserverUser::GetUser(int id) { cout << "在SQL Server 中根据ID得到User表中的一条记录。\n"; return NULL; } class AccessUser :public IUser { public: void Insert(IUser *); IUser * GetUser(int id); }; void AccessUser::Insert(IUser * in) { cout << "在Access给User表增加一条记录。\n"; } IUser * AccessUser::GetUser(int id) { cout << "在Access中根据ID得到User表中的一条记录。\n"; return NULL; } class IFactory { public: virtual IUser * CreateUser() = 0; virtual IDepartment * CreateDepartment() = 0; }; class SqlserverFactory :public IFactory { public: IUser * CreateUser(); IDepartment * CreateDepartment(); }; IUser * SqlserverFactory::CreateUser() { return new SqlserverUser; } IDepartment * SqlserverFactory::CreateDepartment() { return new SqlserverDepartment; } class AccessFactory :public IFactory { public: IUser * CreateUser(); IDepartment * CreateDepartment(); }; IUser * AccessFactory::CreateUser() { return new AccessUser; } IDepartment * AccessFactory::CreateDepartment() { return new AccessDepartment; } #endif
#include"AbstractFactory.h" int main() { IUser * userI = new SqlserverUser; IDepartment * dptI = new SqlserverDepartment; IFactory * factory = new SqlserverFactory; IUser * user = factory->CreateUser(); //IDepartment * dpt = factory->CreateDepartment(); user->Insert(userI); user->GetUser(1); IDepartment * dpt = factory->CreateDepartment(); dpt->Insert(dptI); dpt->GetDepartment(1); IDepartment * AdptI = new AccessDepartment; IFactory * Afactory = new AccessFactory; IDepartment * IDf=Afactory->CreateDepartment(); IDf->Insert(AdptI); IDf->GetDepartment(1); IUser *AuserI = new AccessUser; IUser * AUf = Afactory->CreateUser(); AUf->Insert(AuserI); AUf->GetUser(1); return 0; }
时间: 2024-11-08 15:38:54