1.简单工厂模式
#include <bits/stdc++.h> using namespace std; class produce { private: int width; int height; public: produce(int width,int height):width(width) , height(height){} virtual ~produce(){} }; class A : public produce { public: A(int width, int height) : produce(width, height) { cout << "工厂生产了一件商品 A" << endl; } }; class B : public produce { public: B(int width, int height) : produce(width, height) { cout << "工厂生产了一件商品 B" << endl; } }; class C : public produce { public: C(int width, int height) : produce(width, height) { cout << "工厂生产了一件商品 C" << endl; } }; class factory { public: produce* create_product(string type) { produce* new_product = nullptr; if (type == "A") new_product = new A(50, 50); else if (type == "B") new_product = new B(100, 100); else if (type == "C") new_product = new C(200, 200); return new_product; } }; int main() { factory fact; produce* p1 = fact.create_product("A"); produce* p2 = fact.create_product("B"); produce* p3 = fact.create_product("C"); delete p1; delete p2; delete p3; system("pause"); return 0; }
2.工厂方法模式
#include <bits/stdc++.h> using namespace std; class produce { private: int width; int height; public: produce(){} virtual ~produce(){} }; class A : public produce { public: A() : produce() { cout << "工厂生产了一件商品 A" << endl; } }; class B : public produce { public: B() : produce() { cout << "工厂生产了一件商品 B" << endl; } }; class C : public produce { public: C() : produce() { cout << "工厂生产了一件商品 C" << endl; } }; class factory_father { public: virtual produce* create_produce() = 0; virtual ~factory_father() {}; }; class A_factory:public factory_father { public: virtual produce* create_produce() { return new A(); } }; class B_factory:public factory_father { public: virtual produce* create_produce() { return new B(); } }; class C_factory:public factory_father { public: virtual produce* create_produce() { return new C(); } }; int main() { factory_father* p1 = new A_factory(); p1->create_produce(); factory_father* p2 = new B_factory(); p2->create_produce(); factory_father* p3 = new C_factory(); p3->create_produce(); system("pause"); return 0; }
3.抽象工厂模式
#include <bits/stdc++.h> using namespace std; class produce { private: int width; int height; public: produce(){} virtual ~produce(){} }; class factory_father { public: virtual produce* create_produce_A() = 0; virtual produce* create_produce_B() = 0; virtual produce* create_produce_C() = 0; virtual ~factory_father() {}; }; class this_A : public produce { public: this_A() :produce() { cout << "this平台生产了A" << endl; } }; class this_B : public produce { public: this_B() :produce() { cout << "this平台生产了B" << endl; } }; class this_C : public produce { public: this_C() :produce() { cout << "this平台生产了C" << endl; } }; class that_A : public produce { public: that_A() :produce() { cout << "that平台生产了A" << endl; } }; class that_B : public produce { public: that_B() :produce() { cout << "that平台生产了B" << endl; } }; class that_C : public produce { public: that_C() :produce() { cout << "that平台生产了C" << endl; } }; class this_factory : public factory_father { public: virtual produce* create_produce_A() { return new this_A(); } virtual produce* create_produce_B() { return new this_B(); } virtual produce* create_produce_C() { return new this_C(); } }; class that_factory : public factory_father { public: virtual produce* create_produce_A() { return new that_A(); } virtual produce* create_produce_B() { return new that_B(); } virtual produce* create_produce_C() { return new that_C(); } }; int main() { factory_father* p1 = new this_factory(); p1->create_produce_A(); p1->create_produce_B(); p1->create_produce_C(); factory_father* p2 = new that_factory(); p2->create_produce_A(); p2->create_produce_B(); p2->create_produce_C(); system("pause"); return 0; }
标签:int,create,factory,模式,工厂,produce,height,public,三大 From: https://www.cnblogs.com/yzqiang/p/17686045.html