C++設計模式_工廠模式
阿新 • • 發佈:2019-02-02
工廠模式是將帶有繼承於基類的子類的建立過程交給一個工廠來建立,通過賦予不同的建立標識來建立不同的子類的模式。C++有三種工廠模式:簡單工廠模式、工廠模式和抽象工廠模式簡單工廠模式:現在有一個工廠要生產幾種產品,這個簡單工廠自己可以判斷生產哪一種產品程式碼如下:
工廠模式:不只有如何生產一個工廠,總工廠只負責指揮,子工廠決定程式碼如下:#include <iostream> #include <string> #include <vld.h> using namespace std; class Product { public: Product(string name):_name(name){} virtual void Show() = 0; virtual ~Product(){} protected: string _name; }; class Product_A:public Product { public: Product_A(string name):Product(name){} virtual void Show() { cout << "Product_A::name:" << _name << endl; } }; class Product_B:public Product { public: Product_B(string name):Product(name){} virtual void Show() { cout << "Product_B::name:" << _name << endl; } }; class Factory { public: Factory(){} Product* CreateProduct(int flag) { switch (flag) { case 1: return new Product_A("A"); break; case 2: return new Product_B("B"); break; default: break; } } }; int main() { Factory f; Product* p1 = f.CreateProduct(1); p1->Show(); Product* p2 = f.CreateProduct(2); p2->Show(); delete p1; delete p2; return 0; }
抽象工廠模式:程式碼如下:#include <iostream> #include <string> #include <vld.h> using namespace std; class Product { public: Product(string name):_name(name){} virtual void Show() = 0; virtual ~Product(){} protected: string _name; }; class Product_A:public Product { public: Product_A(string name):Product(name){} virtual void Show() { cout << "Product_A::name:" << _name << endl; } }; class Product_B:public Product { public: Product_B(string name):Product(name){} virtual void Show() { cout << "Product_B::name:" << _name << endl; } }; class Factory { public: virtual Product* CreateProduct() = 0; }; class Factory_A:public Factory { public: Product* CreateProduct() { return new Product_A("A"); } }; class Factory_B:public Factory { public: Product* CreateProduct() { return new Product_B("B"); } };
#include <iostream> #include <string> #include <vld.h> using namespace std; class Product_A { public: Product_A(string name):_name(name){} ~Product_A(){} virtual void Operator() = 0; protected: string _name; }; class Product_A1:public Product_A { public: Product_A1(string name):Product_A(name){} void Operator() { cout << "Product_A1::Operator()" << endl; } }; class Product_A2:public Product_A { public: Product_A2(string name):Product_A(name){} void Operator() { cout << "Product_A2::Operator()" << endl; } }; class Product_B { public: Product_B(string name):_name(name){} ~Product_B(){} virtual void Operator() = 0; protected: string _name; }; class Product_B1:public Product_B { public: Product_B1(string name):Product_B(name){} void Operator() { cout << "Product_B1::Operator()" << endl; } }; class Product_B2:public Product_B { public: Product_B2(string name):Product_B(name){} void Operator() { cout << "Product_B2::Operator()" << endl; } }; class Factory { public: virtual Product_A* CreateProductA() = 0; virtual Product_B* CreateProductB() = 0; }; class Factory_1:public Factory { public: Product_A* CreateProductA() { return new Product_A1("A1"); } Product_B* CreateProductB() { return new Product_B1("B1"); } }; class Factory_2:public Factory { public: Product_A* CreateProductA() { return new Product_A2("A2"); } Product_B* CreateProductB() { return new Product_B2("B2"); } }; int main() { Factory_2 f2; Product_A* p = f2.CreateProductA(); p->Operator(); delete p; return 0; }