设计模式实现系列-简单工厂模式-工厂模式-抽象工厂模式

简单工厂模式

#include <iostream> using namespace std; class Product2 { public: virtual int Behavior() = 0; }; class Product2A:public Product2 { public: int Behavior() { cout << "Product2A finished/n"; return 1; } }; class Product2B:public Product2 { public: int Behavior() { cout << "Product2B finished/n"; return 1; } }; class Product1 { public: virtual int Behavior() = 0; }; class Product1A:public Product1 { public: int Behavior() { cout << "Product1A finished/n"; return 1; } }; class Product1B:public Product1 { public: int Behavior() { cout << "Product1B finished/n"; return 1; } }; class Factory { public: virtual Product1* CreateProduct1() = 0; virtual Product2* CreateProduct2() = 0; }; class FactoryA:public Factory { public: Product2* CreateProduct2() { return new Product2A(); } Product1* CreateProduct1() { return new Product1A(); } }; class FactoryB:public Factory { public: Product2* CreateProduct2() { return new Product2B(); } Product1* CreateProduct1() { return new Product1B(); } }; int main() { Factory* factory = new FactoryA(); Product1 *product1 = factory->CreateProduct1(); product1->Behavior(); delete product1; Product2 *product2 = factory->CreateProduct2(); product2->Behavior(); delete product2; delete factory; return 1; }

 

 

工厂模式

#include <iostream> using namespace std; class Operation { public: virtual int Behavior() = 0; }; class OperationA:public Operation { public: int Behavior() { cout << "OperationA finished"; return 1; } }; class OperationB:public Operation { public: int Behavior() { cout << "OperationB finished"; return 1; } }; class SimpleFactory { public: static Operation* Factory(const char& a) { switch(a) { case 'A': return new OperationA(); break; case 'B': return new OperationB(); break; } } }; int main() { char a; cin >> a; Operation* p = SimpleFactory::Factory(a); p->Behavior(); delete p; return 1; }

 

 

抽象工厂模式 

#include <iostream> using namespace std; class Operation { public: virtual int Behavior() = 0; }; class OperationA:public Operation { public: int Behavior() { cout << "OperationA finished"; return 1; } }; class OperationB:public Operation { public: int Behavior() { cout << "OperationB finished"; return 1; } }; class Factory { public: virtual Operation* CreateOperation() = 0; }; class FactoryA:public Factory { public: Operation* CreateOperation() { return new OperationA(); } }; class FactoryB:public Factory { public: Operation* CreateOperation() { return new OperationB(); } }; int main() { Factory* p = new FactoryB(); Operation *q = p->CreateOperation(); q->Behavior(); delete p; delete q; return 1; }

 

 

抽象工厂经过简单工厂修饰

#include <iostream> using namespace std; class Product2 { public: virtual int Behavior() = 0; }; class Product2A:public Product2 { public: int Behavior() { cout << "Product2A finished/n"; return 1; } }; class Product2B:public Product2 { public: int Behavior() { cout << "Product2B finished/n"; return 1; } }; class Product1 { public: virtual int Behavior() = 0; }; class Product1A:public Product1 { public: int Behavior() { cout << "Product1A finished/n"; return 1; } }; class Product1B:public Product1 { public: int Behavior() { cout << "Product1B finished/n"; return 1; } }; class Factory { public: static Factory* CreateFactory(const char&); virtual Product1* CreateProduct1() = 0; virtual Product2* CreateProduct2() = 0; }; class FactoryA:public Factory { public: Product2* CreateProduct2() { return new Product2A(); } Product1* CreateProduct1() { return new Product1A(); } }; class FactoryB:public Factory { public: Product2* CreateProduct2() { return new Product2B(); } Product1* CreateProduct1() { return new Product1B(); } }; Factory* Factory::CreateFactory(const char&a) { switch(a) { case 'A': return new FactoryA(); break; case 'B': return new FactoryB(); break; } } int main() { Factory* factory = Factory::CreateFactory('B'); Product1 *product1 = factory->CreateProduct1(); product1->Behavior(); delete product1; Product2 *product2 = factory->CreateProduct2(); product2->Behavior(); delete product2; delete factory; return 1; }

你可能感兴趣的:(设计模式,delete,Class,behavior)