C++实现简单工厂模式
2012-11-01 08:43
405 查看
/* 简单工厂模式:跟工厂模式很类似,不过在函数里面加上了逻辑判断 Created by Phoenix_FuliMa */ #include <iostream> using namespace std; class Product { public: virtual void display() = 0; }; class Product1:public Product { public: void display() { cout<<"I am product1..."<<endl; } }; class Product2:public Product { public: void display() { cout<<"I am product2..."<<endl; } }; class Factory { public: Product *CreateProduct(const char *type) { if(strcmp(type, "product1") == 0) { return new Product1; } else if(strcmp(type, "product2") == 0) { return new Product2; } else { return NULL; } } }; int main() { Factory *fac = new Factory(); Product *product1 = fac->CreateProduct("product1"); Product *product2 = fac->CreateProduct("product2"); product1->display(); product2->display(); system("pause"); return 0; }
相关文章推荐
- 大话设计模式c++实现之一 简单工厂模式
- 设计模式之简单工厂C++实现
- 简单工厂模式 C++实现
- 设计模式 -- 简单工厂模式 -- c++实现
- 简单工厂模式 c++实现
- 设计模式的C++实现 24.简单工厂模式
- 大话设计模式之C++实现---简单工厂模式
- 简单工厂模式 C++实现
- 设计模式之简单工厂模式:c++实现
- 《大话设计模式》读书笔记(C++代码实现) 第一章:简单工厂模式
- 简单工厂设计模式C++实现
- 大话设计模式(C++实现)--简单工厂模式
- 简单工厂模式-C++实现
- 设计模式 简单工厂 c++实现
- 《大话设计模式》读书笔记(C++代码实现) 第一章:简单工厂模式
- 策略模式与简单工厂的C++实现
- 设计模式C++实现一:简单工厂模式
- C++实现简单工厂模式
- 简单工厂模式——MFC计算器实现(连续操作)
- 用C++学习设计模式之 简单工厂模式