C++设计模式从0进击-2-抽象工厂模式
抽象工厂模式(Abstract Factory)
//用户抽象接口 class IUser { public: virtual void GetUser() = 0; virtual void InsertUser() = 0; }; //部门抽象接口 class IDepartment { public: virtual void GetDepartment() = 0; virtual void InsertDepartment() = 0; }; //ACESS用户 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 Insert User" << endl; } }; //SQL部门 class CSqlDepartment: public IDepartment { public: virtual void GetDepartment() { cout << "Sql Department" << endl; } virtual void InsertDepartment() { cout << "Sql Insert Department" << 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(); } }; int _tmain(int argc, _TCHAR * argv[]) { IFactory * factory = new SQLFactory(); IUser * user = factory->CreateUser(); IDepartment * depart = factory->CreateDepartment(); user->GetUser(); depart->GetDepartment(); return 0; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。