设计模式之简单工厂+普通工厂+抽象工厂C++实现
这破设计模式不自己写一下,看书真是感觉像天书。自己写一下,是TM真简单。。
简单工厂:
#include<iostream> using namespace std; //产品类(抽象类,不能实例化) class Product{ public: Product(){} virtual void show()=0; //纯虚函数 }; //产品1 class Product_A:public Product{ public: Product_A(){} void show(){ cout<<"This is Product_A"<<endl; } }; //产品2 class Product_B:public Product{ public: Product_B(){} void show(){ cout<<"This is Product_B"<<endl; } }; class SimpleFactory{ public: Product* CreateProduct(string); }; Product* SimpleFactory::CreateProduct(string Name){//简单工厂:在工厂内部通过逻辑判断,来生产不同类型的产品 Product* _Product=nullptr; if(Name=="A"){ _Product= new Product_A(); } else{ _Product=new Product_B(); } return _Product; } int main(){ SimpleFactory MyFactor; auto _Product=MyFactor.CreateProduct("A"); _Product->show(); delete _Product; _Product=MyFactor.CreateProduct("B"); _Product->show(); delete _Product; getchar(); return 0; }
普通工厂:
#include<iostream> using namespace std; //--------------------------------------- //产品类(抽象类,不能实例化) class Product{ public: Product(){} virtual void show()=0; //纯虚函数 }; //产品1 class Product_A:public Product{ public: Product_A(){} void show(){ cout<<"This is Product_A"<<endl; } }; //产品2 class Product_B:public Product{ public: Product_B(){} void show(){ cout<<"This is Product_B"<<endl; } }; //------------------------- class Factory{//抽象类 public: virtual Product* CreateProduct()=0; }; class Factor_A:public Factory{//工厂类A,只生产A产品 public: Product* CreateProduct(){ Product* _Product=nullptr; _Product=new Product_A(); return _Product; } }; class Factor_B:public Factory{//工厂类B,只生产B产品 public: Product* CreateProduct(){ Product* _Product=nullptr; _Product= new Product_B(); return _Product; } }; int main(){ Product* _Product=nullptr; auto MyFactory_A=new Factor_A(); _Product=MyFactory_A->CreateProduct();// 调用产品A的工厂来生产A产品 _Product->show(); delete _Product; auto MyFactory_B=new Factor_B(); _Product=MyFactory_B->CreateProduct();// 调用产品B的工厂来生产B产品 _Product->show(); delete _Product; getchar(); return 0; }
抽象工厂:
#include<iostream> using namespace std; //--------------------------------------- class Product_1{//产品类1,有两个子类:A1、B1 public: Product_1(){} virtual void show()=0; //纯虚函数 }; //产品A_1 class Product_A_1:public Product_1{ public: Product_A_1(){} void show(){ cout<<"This is Product_A_1"<<endl; } }; //产品B_1 class Product_B_1:public Product_1{ public: Product_B_1(){} void show(){ cout<<"This is Product_B_1"<<endl; } }; class Product_2{//产品类2,有两个子类:A2、B2 public: Product_2(){} virtual void show()=0; //纯虚函数 }; //产品A_2 class Product_A_2:public Product_2{ public: Product_A_2(){} void show(){ cout<<"This is Product_A_2"<<endl; } }; //产品B_2 class Product_B_2:public Product_2{ public: Product_B_2(){} void show(){ cout<<"This is Product_B_2"<<endl; } }; //------------------------- //抽象工厂类,有两个子工厂类:工厂A、工厂B class AbstractFactory{ public: virtual Product_1* CreateProduct_1()=0; virtual Product_2* CreateProduct_2()=0; }; class Factory_A:public AbstractFactory{ //工厂类A,生产A1+A2 public: Product_1* CreateProduct_1(){ Product_1* _Product_1=nullptr; _Product_1=new Product_A_1(); return _Product_1; } Product_2* CreateProduct_2(){ Product_2* _Product_2=nullptr; _Product_2=new Product_A_2(); return _Product_2; } }; class Factory_B:public AbstractFactory{ //工厂类B,生产B1+B2 public: Product_1* CreateProduct_1(){ Product_1* _Product_1=nullptr; _Product_1=new Product_B_1(); return _Product_1; } Product_2* CreateProduct_2(){ Product_2* _Product_2=nullptr; _Product_2=new Product_B_2(); return _Product_2; } }; int main(){ AbstractFactory* _AbstractFactory=nullptr; _AbstractFactory=new Factory_A();//指向工厂A Product_1* _Product_1=nullptr; Product_2* _Product_2=nullptr; _Product_1=_AbstractFactory->CreateProduct_1();//工厂A生产A_1产品 _Product_1->show(); delete _Product_1; _Product_2= _AbstractFactory->CreateProduct_2();//工厂A生产A_2产品 _Product_2->show(); delete _Product_2; delete _AbstractFactory; _AbstractFactory=new Factory_B();//指向工厂B _Product_1=_AbstractFactory->CreateProduct_1();//工厂B生产B_1产品 _Product_1->show(); delete _Product_1; _Product_2= _AbstractFactory->CreateProduct_2();//工厂B生产B_2产品 _Product_2->show(); delete _Product_2; delete _AbstractFactory; getchar(); return 0; }
进击的小🐴农