C++简单工厂模式(Simple Factory Pattern)实现
来源:互联网 发布:java内存溢出 编辑:程序博客网 时间:2024/06/01 17:55
#ifndef CHART_H#define CHART_H#include <iostream>#include <string>class Chart{ public: virtual void display() = 0; };class HistogramChart : public Chart{ public: HistogramChart() { std::cout << "Historgram constructor" << std::endl; } void display() { std::cout << "show Histogram" << std::endl; }};class PieChart: public Chart{ public: PieChart() { std::cout << "Piechart constructor" << std::endl; } void display() { std::cout << "show Piechart" << std::endl; }};class LineChart: public Chart{ public: LineChart() { std::cout << "Linechart constructor" << std::endl; } void display() { std::cout << "show Linechart" << std::endl; }};class ChartFactory{ public: static Chart* getChart(const std::string& strName) { Chart* p = NULL; if (strName == "histogramchart") { p = new HistogramChart; } else if (strName == "piechart") { p = new PieChart; } else if (strName == "linechart") { p = new LineChart; } return p; }};#endif
#include "Chart.h"void test(){ Chart* p = ChartFactory::getChart(std::string("histogramchart")); p->display(); p = ChartFactory::getChart(std::string("piechart")); p->display(); p = ChartFactory::getChart(std::string("linechart")); p->display();}int main(){ test();}
0 0
- C++简单工厂模式(Simple Factory Pattern)实现
- 简单工厂模式(simple Factory Pattern)
- 简单工厂模式(simple factory pattern)
- 简单工厂模式(Simple Factory Pattern)
- 简单工厂模式(Simple Factory Pattern)
- 简单工厂模式(simple factory pattern)
- 简单工厂模式(Simple Factory Pattern)
- 设计模式之简单工厂模式(Simple Factory Pattern)
- 设计模式1:Simple Factory Pattern(简单工厂模式)
- 【JAVA设计模式】简单工厂模式(Simple Factory Pattern)
- 设计模式(1):简单工厂模式(Simple Factory Pattern)
- Java设计模式:简单工厂模式(Simple Factory Pattern)
- 简单工厂模式(Simple Factory Pattern)
- 简单工厂模式 Simple Factory Pattern
- 一、简单工厂模式(Simple Factory Pattern)
- 简单工厂模式(Simple Factory Pattern)
- 1.简单工厂模式 Simple Factory Pattern
- 简单工厂模式 Simple Factory Pattern
- 欢迎使用CSDN-markdown编辑器
- web常用英语词汇
- VSCode c++配置文件
- Activity启动过程分析
- QT学习之路四(信号与槽----通讯录的再次改版)
- C++简单工厂模式(Simple Factory Pattern)实现
- Hadoop集群搭建
- mysql 分表,分区,分库相关及merge引擎
- 2017.2.14 日课
- 某付数据科技公司的一道面试附加题
- 哪种男人对女人最有吸引力
- Android Recyclerview 万能分割线
- springMVC注解@initbinder日期类型的属性自动转换
- 数据分析之旅(四)