C++实现抽象工厂模式教程
程序员文章站
2022-04-27 19:09:31
C++实现抽象工厂模式教程
#include
using namespace std;
//抽象产品类
class AbstractProduct{
publi...
C++实现抽象工厂模式教程
#include using namespace std; //抽象产品类 class AbstractProduct{ public: virtual void ProductFunction() = 0; //子类可以重写该函数来实现不同子类的特有功能 virtual ~AbstractProduct(){} }; //具体产品类,有实现自己功能的成员函数 class ConcreteProductA:public AbstractProduct{ public: void ProductFunction() { cout << "ConcreteProductA do its function" << endl; } }; class ConcreteProductB:public AbstractProduct{ public: void ProductFunction() { cout << "ConcreteProductB do its function" << endl; } }; //抽象工厂类,可以生产不同的产品 class AbstractFactory{ public: virtual AbstractProduct* CreateProduct() = 0; virtual ~AbstractFactory(){} }; class ConcreteFactoryA:public AbstractFactory{ public: AbstractProduct* CreateProduct() { return new ConcreteProductA(); } }; class ConcreteFactoryB:public AbstractFactory{ public: AbstractProduct* CreateProduct() { return new ConcreteProductB(); } }; int main(int argc,char** argv) { AbstractFactory *factoryA = new ConcreteFactoryA(); AbstractFactory *factoryB = new ConcreteFactoryB(); AbstractProduct *productA = factoryA->CreateProduct(); AbstractProduct *productB = factoryB->CreateProduct(); productA->ProductFunction(); productB->ProductFunction(); return 0; }
以下是书中具体的一个DB例子
#include using namespace std; class User{ }; class IUser{ public: virtual void Insert(User user) = 0; virtual void GetId(User user) = 0; }; class SqlServerUser:public IUser{ public: void Insert(User user) { cout << "use sqlserver insert into user" << endl; } void GetId(User user) { cout << "use sqlserver get user id " << endl; } }; class AccessUser:public IUser{ public: void Insert(User user) { cout << "use AccessUser insert into user" << endl; } void GetId(User user) { cout << "use AccessUser get user id " << endl; } }; class IFactory{ public: virtual IUser* CreateUser() = 0; }; class SqlServerFactory:public IFactory{ public: IUser* CreateUser() { return new SqlServerUser(); } }; class AcceseFactory:public IFactory{ public: IUser* CreateUser() { return new AccessUser(); } }; int main(int argc,char** argv) { User user; IFactory* access = new AcceseFactory(); IUser* accessUser = access->CreateUser(); accessUser->Insert(user); return 0; }
利用简单工厂来改进抽象工厂,也就是将选择工厂转移到类中决定,而不必通过继承的各种子类来决定
#include #include using namespace std; //抽象产品类 class AbstractProduct{ public: virtual void ProductFunction() = 0; //子类可以重写该函数来实现不同子类的特有功能 virtual ~AbstractProduct(){} }; //具体产品类,有实现自己功能的成员函数 class ConcreteProductA:public AbstractProduct{ public: void ProductFunction() { cout << "ConcreteProductA do its function" << endl; } }; class ConcreteProductB:public AbstractProduct{ public: void ProductFunction() { cout << "ConcreteProductB do its function" << endl; } }; //产品枚举类,代表不同产品 enum ProductType{ ConcreteA, ConcreteB }; //抽象工厂类,可以生产不同的产品 class Factory{ public: static AbstractProduct* CreateProduct() { switch (m_ProductType) { case ConcreteA: return new ConcreteProductA(); break; case ConcreteB: return new ConcreteProductB(); break; default: return nullptr; break; } } private: static ProductType m_ProductType; }; //在类外初始化static数据成员并决定生成哪种产品 ProductType Factory::m_ProductType = ConcreteB; int main(int argc,char** argv) { AbstractProduct *product = Factory::CreateProduct(); product->ProductFunction(); return 0; }
下一篇: C语言入门(十三)函数参数的传递和值返回