首页 > 代码库 > simple_factory
simple_factory
#include <stdlib.h>#include <iostream>using namespace std;class Product{public: virtual ~Product() {} virtual int Interface()=0;};class ConcreteProductA : public Product{public: ConcreteProductA() {} virtual ~ConcreteProductA() {} int Interface() { cout<<"Make my ProductA good!"<<endl; return -1; }};class ConcreteProductB : public Product{public: ConcreteProductB() {} virtual ~ConcreteProductB() {} int Interface() { cout<<"Make my ProductB better!"<<endl; return 0; } };class ConcreteProductC : public Product{public: ConcreteProductC() {} virtual ~ConcreteProductC() {} int Interface() { cout<<"Make my ProductC best!"<<endl; return 1; } };#define PRODUCTA 1#define PRODUCTB 2#define PRODUCTC 3class SimpleFactory{public: static Product* CreateProduct(int iType) { switch(iType) { case PRODUCTA: return new ConcreteProductA; case PRODUCTB: return new ConcreteProductB; case PRODUCTC: return new ConcreteProductC; default: { cerr<<"Sorry, I have not the product you need!"<<endl; return NULL; } } }};#define CUSTOMER main#define DESTROY(ptr) if (ptr) { delete ptr; ptr = NULL; }int CUSTOMER(int argc, char** argv){ Product* pProduct = NULL; pProduct = SimpleFactory::CreateProduct(PRODUCTA); pProduct->Interface(); DESTROY(pProduct); pProduct = SimpleFactory::CreateProduct(PRODUCTB); pProduct->Interface(); DESTROY(pProduct); pProduct = SimpleFactory::CreateProduct(PRODUCTC); pProduct->Interface(); DESTROY(pProduct); return EXIT_SUCCESS;}
simple_factory
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。