simple_factory
#include <stdlib.h>
#include <iostream>
using namespace std; class Product
{
public:
virtual ~Product() {}
virtual int Interface()=;
}; class ConcreteProductA : public Product
{
public:
ConcreteProductA() {}
virtual ~ConcreteProductA() {} int Interface()
{
cout<<"Make my ProductA good!"<<endl;
return -;
}
}; class ConcreteProductB : public Product
{
public:
ConcreteProductB() {}
virtual ~ConcreteProductB() {} int Interface()
{
cout<<"Make my ProductB better!"<<endl;
return ;
}
}; class ConcreteProductC : public Product
{
public:
ConcreteProductC() {}
virtual ~ConcreteProductC() {} int Interface()
{
cout<<"Make my ProductC best!"<<endl;
return ;
}
}; #define PRODUCTA 1
#define PRODUCTB 2
#define PRODUCTC 3 class 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的更多相关文章
- Java设计模式(一) 简单工厂模式不简单
摘要:本文介绍了简单工厂模式的概念,优缺点,实现方式,以及结合Annotation和反射的改良方案(让简单工厂模式不简单).同时介绍了简单工厂模式(未)遵循的OOP原则.最后给出了简单工厂模式在JDB ...
- 【创建型】Abstract Factory模式 & Factory Method模式
本文主要介绍简单工厂模式与抽象工厂模式.以下如有叙述不当之处,欢迎批评指正,欢迎交流探讨. 一:简单工厂模式 在23种设计模式中,简单工厂模式其实是不在列的,但理解该模式对于抽象工厂模式的思想理解是有 ...
- 【51】java设计模式-工厂设计模式剖析
工厂设计设计模式的分类: 工厂模式在<Java与模式>中分为三类: 1)简单工厂模式(Simple Factory):不利于产生系列产品: 2)工厂方法模式(Factory Method) ...
随机推荐
- 类的this指针 总结
类的this指针有以下特点: (1)this只能在成员函数中使用 全局函数,静态函数都不能使用this. 实际上,成员函数默认第一个参数为T* const this. 如: class A { pub ...
- mysql索引需要了解的几个注意
板子之前做过2年web开发培训(入门?),获得挺多学生好评,这是蛮有成就感的一件事,准备花点时间根据当时的一些备课内容整理出一系列文章出来,希望能给更多人带来帮助,这是系列文章的第一篇 注:科普文章一 ...
- AngularJS - Watch 监听对象
<body> <div ng-app="myApp"> <div ng-controller="firstController"& ...
- mysql高效获取两张表共同字段的交集数据
例如下面两站表A,B.A表和B表分别有5-10w数据.A表结构如下:id bid name title publisher extraB表结构如下id bid name title publisher ...
- 探索 ConcurrentHashMap 高并发性的实现机制--转
ConcurrentHashMap 是 Java concurrent 包的重要成员.本文将结合 Java 内存模型,来分析 ConcurrentHashMap 的 JDK 源代码.通过本文,读者将了 ...
- python学习笔记 class
在这段时间中学习python在里面绕来绕去晕了. class和其他的语言都差不多,但是我理解中其实class和function差不多,只是将function封装起来. 在python中第一个class ...
- C# WinForm 中ComboBox数据绑定的问题 (转)
来自:http://blog.sina.com.cn/s/blog_5fb9e26301013wga.html C# WinForm 中ComboBox数据绑定的问题 怎样让WinForm中的Comb ...
- JAVA实现DES加密
DES加密介绍 DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法.DES加密算法出自IBM的研究,后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少 ...
- python(4)-迭代器 和 生成器
迭代器是访问集合元素的一种方式.迭代器适合遍历一些巨大或无限的集合,比如几个G的文件.迭代器具有以下特点: 1. 访问者不需要关心迭代器内部的结构,只需通过__next__()方法不断取下一个内容 2 ...
- XML XmlDocument
1.SelectSingleNode(xPath) xPxth:xml/cam/type 一层一层递进