#ifndef __ABSTRACT_FACTORY_MODEL__ #define __ABSTRACT_FACTORY_MODEL__ #include <string> #include <vector> #include <iostream> using namespace std; /* GOOD:定义了一个创建一系列相关或相互依赖的接口,而无需指定它们的具体类。 用于交换产品系列,如ACCESS->SQL SERVER; 产品的具体类名被具体工厂的实现分离 */ //用户抽象接口 class IUser { public: virtual void getUser() = 0; virtual void insertUser() = 0; }; //部门抽象接口 class IDepartment { public: virtual void getDepartment() = 0; virtual void insertDepartment() = 0; }; //ACCESS用户 class CAccessUser : public IUser { public: virtual void getUser(){ cout<<"Access GetUser"<<endl; } virtual void insertUser(){ cout<<"Access InsertUser"<<endl; } }; //ACCESS部门 class CAccessDepartment : public IDepartment { public: virtual void getDepartment(){ cout<<"Access GetDepartment"<<endl; } virtual void insertDepartment(){ cout<<"Access InsertDepartment"<<endl; } }; //SQL用户 class CSqlUser : public IUser { public: virtual void getUser(){ cout<<"Sql User"<<endl; } virtual void insertUser(){ cout<<"Sql User"<<endl; } }; //SQL部门类 class CSqlDepartment : public IDepartment { public: virtual void getDepartment(){ cout<<"Sql GetDepartment"<<endl; } virtual void insertDepartment(){ cout<<"Sql InsertDepartment"<<endl; } }; //抽象工厂 class IFactory { public: virtual IUser* createUser() = 0; virtual IDepartment* createDepartment() = 0; }; //ACCESS 工厂 class AccessFactory : public IFactory { public: virtual IUser* createUser(){ return new CAccessUser(); } virtual IDepartment* createDepartment(){ return new CAccessDepartment(); } }; //SQL 工厂 class SqlFactory : public IFactory { public: virtual IUser* createUser(){ return new CSqlUser(); } virtual IDepartment* createDepartment(){ return new CSqlDepartment(); } }; #endif //__ABSTRACT_FACTORY_MODEL__ /* #include "AbstractFactoryModel.h" int _tmain(int argc, _TCHAR* argv[]) { IFactory* factory = new SqlFactory(); IUser* user = factory->createUser(); IDepartment* depart = factory->createDepartment(); user->getUser(); depart->getDepartment(); return 0; } */