c++设计模式之工厂方法模式
来源:互联网 发布:python能做成包吗 编辑:程序博客网 时间:2024/06/07 18:00
工厂方法模式定义了一个创建对象的接口, 由子类决定实例化的类是哪一个
工厂方法模式将子类的实例化推迟到了子类
所有的工厂模式都是封装对象的创建. 工厂方法模式让子类决定创建的对象是什么
工厂方法模式使用继承将对象的创建委托给子类, 子类实现工厂方法来创建对象
#ifndef FactoryMethod_h#define FactoryMethod_hclass Product // 产品抽象类{public:virtual void method() = 0;};class ProductA : public Product // 具体产品A{public:virtual void method(){printf("ProductA method.\n");}};class ProductB : public Product // 具体产品B{public:virtual void method(){printf("ProductB method.\n");}};class Factory // 工厂的抽象类{public:virtual Product *createProduct() = 0;};class FactoryA : public Factory{public:virtual Product *createProduct(){return new ProductA();}};class FactoryB : public Factory{public:virtual Product *createProduct(){return new ProductB();}};#endif
测试
#include <iostream>#include "FactoryMethod.h"int main(int argc, char **argv){FactoryA factoryA;FactoryB factoryB;Product *productA = factoryA.createProduct();productA->method();Product *productB = factoryB.createProduct();productB->method();return 0;}
0 0
- C#--设计模式之工厂方法模式
- 设计模式之简单工厂,工厂方法模式(c++)
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式 之 工厂方法
- 设计模式 之 工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- 设计模式之工厂方法
- UVA 12124 Assemble (二分 + 字典树)
- JavaScript函数小结
- 配置java环境变量和maven环境变量
- Hadoop报错
- select函数的使用
- c++设计模式之工厂方法模式
- iOS 封装WKWebView页面带加载进度条
- 统计图分析(源自Echars图形)——饼图
- C#中如何加载dll并调用其函数
- BIJI
- 嵌入式linux自动更新网络时间NTP移植
- 摄像头驱动3_虚拟驱动vivi彻底分析
- free 命令详解
- 20161205