Factory 模式

用户不关心工厂的具体类型,只知道这是一个工厂就行。

通过工厂的实现推迟到子类里面去来确定工厂的具体类型。

工厂的具体类型来确定生产的具体产品。

同时用户不关心这是一个什么样子的产品,只知道这是一个产品

 #ifndef _FACTORY_H_
#define _FACTORY_H_ #include "product.h"
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" Factory::~Factory()
{ } Factory::Factory()
{ } Product* Factory::createProduct()
{
} ConcreteFactory::ConcreteFactory()
{ } ConcreteFactory::~ConcreteFactory()
{ } Product* ConcreteFactory::createProduct()
{
return new ConcreteProduct();
}
 #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 <stdio.h>
Product::~Product()
{ } Product::Product()
{ } ConcreteProduct::ConcreteProduct()
{
printf("hello I'm product\n");
} ConcreteProduct::~ConcreteProduct()
{
}
 #include "factory.h"
#include "product.h"
#include <stdio.h> int main()
{
Product* pro = NULL;
Factory* fac = new ConcreteFactory();
if(fac)
pro = fac->createProduct();
return ;
}

设计模式学习笔记 1.factory 模式的更多相关文章

  1. 设计模式学习笔记--备忘录(Mamento)模式

    写在模式学习之前 什么是设计模式:在我们进行程序设计时,逐渐形成了一些典型问题和问题的解决方式,这就是软件模式:每个模式描写叙述了一个在我们程序设计中常常发生的问题,以及该问题的解决方式:当我们碰到模 ...

  2. 设计模式学习笔记-Adapter模式

    Adapter模式,就是适配器模式,使两个原本没有关联的类结合一起使用. 平时我们会经常碰到这样的情况,有了两个现成的类,它们之间没有什么联系,但是我们现在既想用其中一个类的方法,同时也想用另外一个类 ...

  3. Java-马士兵设计模式学习笔记-装饰者模式

    Java装饰者模式简介 一.假设有一个Worker接口,它有一个doSomething方法,Plumber和Carpenter都实现了Worker接口,代码及关系如下: 1.Worker.java p ...

  4. 研磨设计模式学习笔记2--外观模式Facade

    需求:客户端需要按照需求,执行一个操作,操作包括一个系统中的3个模块(根据配置选择是否全部执行). 外观模式优点: 客户端无需知道系统内部实现,,只需要写好配置文件,控制那些模块执行,简单易用. 外观 ...

  5. 设计模式学习笔记——Composite 组合模式

    用于描述无限层级的复杂对象,类似于描述资源管理器,抽象出每一个层级的共同特点(文件夹和文件,展开事件) 以前描述一个对象,是将整个对象的全部数据都描述清楚,而组合模式通过在对象中定义自己,描述自己的下 ...

  6. 设计模式学习笔记——Bridge 桥接模式

    先说一下我以前对桥接模式的理解:当每个类中都使用到了同样的属性或方法时,应该将他们单独抽象出来,变成这些类的属性和方法(避免重复造轮子),当时的感觉是和三层模型中的model有点单相似,也就是让mod ...

  7. 设计模式学习笔记——Visitor 访问者模式

    1.定义IVisitor接口,确定变化所涉及的方法 2.封装变化类.实现IVisitor接口 3.在实体类的变化方法中传入IVisitor接口,由接口确定使用哪一种变化来实现(封装变化) 4.在使用时 ...

  8. Java-马士兵设计模式学习笔记-责任链模式-FilterChain功能

    一.目标 增加filterchain功能 二.代码 1.Filter.java public interface Filter { public String doFilter(String str) ...

  9. Java-马士兵设计模式学习笔记-责任链模式-处理数据

    一.目标 数据提交前做各种处理 二.代码 1.MsgProcessor.java public class MsgProcessor { private List<Filter> filt ...

随机推荐

  1. 实习日记 laravel怎么删除磁盘上的文件

    Storage 里面有 delete的方法 具体使用是 Storage::disk('uploads')->delete($fileName); 其中'uploads'是filesystem里面 ...

  2. mysql查询sql中检索条件为大批量数据时处理

    当userIdArr数组值为大批量时,应如此优化代码实现

  3. 05 referer头与防盗链

    像上图中的这个效果,当我们在网页里引用站外图片时,常出现这样的情况. ??? 服务器是怎么样知道,这个图片是在站外被引用的呢? 还有在网站的统计结果,统计用户从何而来,如下图 ??? 统计时,是如何得 ...

  4. 【TensorFlow-windows】(三) 多层感知器进行手写数字识别(mnist)

    主要内容: 1.基于多层感知器的mnist手写数字识别(代码注释) 2.该实现中的函数总结 平台: 1.windows 10 64位 2.Anaconda3-4.2.0-Windows-x86_64. ...

  5. android日历控件

    源码地址 : http://download.csdn.net/detail/abc13939746593/7265459

  6. iOS中从零開始使用protobuf

    让我们一起打开以下这个链接 https://github.com/alexeyxo/protobuf-objc 在github上有protobuf-objc,当中的readme能够教会我们安装prot ...

  7. Erlang function guards NOTE

    Note: I've compared , and ; in guards to the operators andalso and orelse. They're not exactly the s ...

  8. cenos 6.5 安装apache 2.4.28出现种问题

    编译出错 configure: error: APR-util not found. Please read the documentation 解决办法 wget http://apache.fre ...

  9. MongoDB 学习三

    这章我们学习MongoDB的查询操作. Introduction to find find方法用于执行MongoDB的查询操作.它返回collecion中的documents子集,没有添加参数的话它将 ...

  10. Mac下通过命令行安装npm install -g 报错,如何解决?

    1, 使用 sudo npm install -g n2, 或者 sudo chmod -R 777 /usr/local/lib,然后 npm install -g