Factory Pattern
2024-08-23 05:44:49
1.Factory模式两个最重要的功能
(1)定义创建对象的接口,封装了对象的创建。
(2)使得具体化类的工作延迟到了子类中。
2.Factory模式结构示意图
3.实现
#ifndef _PRODUCT_H_
#define _PRODUCT_H_ class Product
{
public:
virtual ~Product() = ;
protected:
Product();
private:
}; class ConcreteProduct:public Product
{
public:
~ConcreteProduct();
ConcreteProduct();
protected:
private:
}; #endif
Product.h
#include "Product.h"
#include <iostream>
using namespace std; Product::Product()
{ }
Product::~Product()
{ } ConcreteProduct::ConcreteProduct()
{
cout<<"ConcreteProduct..."<<endl;
}
ConcreteProduct::~ConcreteProduct()
{ }
Product.cpp
#ifndef _FACTORY_H_
#define _FACTORY_H_ class Product; class Factory
{
public:
virtual ~Factory() = ;
virtual Product* CreateProduct() = ;
protected:
Factory();
private:
}; class ConcreteFactory:public Factory
{
public:
~ConcreteFactory();
ConcreteFactory();
Product* CreateProduct();
protected:
private:
};
#endif
Factory.h
#include "Factory.h"
#include "Product.h" #include <iostream>
using namespace std; Factory::Factory()
{ }
Factory::~Factory()
{ }
ConcreteFactory::ConcreteFactory()
{
cout<<"ConcreteFactory....."<<endl;
}
ConcreteFactory::~ConcreteFactory()
{ }
Product* ConcreteFactory::CreateProduct()
{
return new ConcreteProduct();
}
Factory.cpp
最新文章
- Linux Shell中单引号、双引号、反引号的区别【转载】
- 文件和目录:access函数
- HDU 2082 母函数模板题
- 3----lua的数据转换及运算符
- NSBundle UIImageView &;UIButton
- Linux基本命令(7)文件阅读的命令
- 慎用preg_replace危险的/e修饰符(一句话后门常用)
- SpringMVC文件上传与下载
- C和指针 读书笔记
- $(function(){})的执行过程分析
- 腾讯优图及知脸(ZKface)人脸比对接口测试(python)
- PHP递归遍历指定文件夹内的文件
- Tomcat——目录结构
- PyGame实现情人节表白利器
- 用EventEmitter收发消息
- 使用Spring+MySql实现读写分离(三)主从复制
- es6的模块化--AMD/CMD/commonJS/ES6
- 【转】java 读取 excel 2003 或 excel 2007
- Kivy 中文教程 实例入门 简易画板 (Simple Paint App):0. 项目简介 &; 成果展示
- 页面常见布局以及实现方法--flex
热门文章
- 推荐!手把手教你使用Git(转)
- jquery小技巧:使用jquery.confirm和PNotify实现弹出提示和消息提示
- 遇到的cannot find module &#39;xxx&#39; 问题
- BZOJ——2134: 单选错位
- 洛谷—— P2895 [USACO08FEB]流星雨Meteor Shower
- linux下kill某个应用
- Maven设置代理
- windows10 安装 mysql 5.6 教程
- U-net图像分割
- MBR结构解析与fdisk的bash实现