`
runfeel
  • 浏览: 902863 次
文章分类
社区版块
存档分类
最新评论

我所理解的设计模式(C++实现)——工厂方法模式(Factory Method Pattern)

 
阅读更多

工厂方法模式不同于简单工厂模式的地方在于工厂方法模式把对象的创建过程放到里子类里。这样工厂父对象和产品父对象一样,可以是抽象类或者接口,只定义相应的规范或操作,不涉及具体的创建或实现细节。

其类图如下:


实例代码为:

#pragma once
class IProduct
{
public:
	IProduct(void);
	virtual ~IProduct(void);
};

#pragma once
#include "iproduct.h"
class IPad :
	public IProduct
{
public:
	IPad(void);
	~IPad(void);
};

#pragma once
#include "iproduct.h"
class IPhone :
	public IProduct
{
public:
	IPhone(void);
	~IPhone(void);
};



#pragma once
#include"IProduct.h"

class IFactory
{
public:
	IFactory(void);
	virtual ~IFactory(void);

	virtual IProduct* getProduct();
};


#pragma once
#include "ifactory.h"
class IPadFactory :
	public IFactory
{
public:
	IPadFactory(void);
	~IPadFactory(void);

	virtual IProduct* getProduct();
};


#pragma once
#include "ifactory.h"
class IPhoneFactory :
	public IFactory
{
public:
	IPhoneFactory(void);
	~IPhoneFactory(void);

	virtual IProduct* getProduct();
};

关键的实现:

#include "StdAfx.h"
#include "IPadFactory.h"
#include"IPad.h"

IPadFactory::IPadFactory(void)
{
}


IPadFactory::~IPadFactory(void)
{
}

IProduct* IPadFactory::getProduct()
{
	return new IPad();
}


#include "StdAfx.h"
#include "IPhoneFactory.h"
#include"IPhone.h"

IPhoneFactory::IPhoneFactory(void)
{
}


IPhoneFactory::~IPhoneFactory(void)
{
}


IProduct* IPhoneFactory::getProduct()
{
	return new IPhone();
}

调用方式:

#include "stdafx.h"
#include"IFactory.h"
#include"IPadFactory.h"
#include"IPhoneFactory.h"
#include"IProduct.h"


int _tmain(int argc, _TCHAR* argv[])
{
	IFactory *fac = new IPadFactory();
	IProduct *pro = fac->getProduct();

	fac = new IPhoneFactory();
	pro = fac->getProduct();
	return 0;
}


应用场景:

1..net里面的数据库连接对象就是产生数据命令对象的工厂。每种数据库的connection对象里(继承自IDbConnection)都有对自己createCommand(定义在IDbCommand里)的实现。

2..net里面的迭代器,IEnumerable定义了迭代器的接口,即工厂方法,每一个继承自IEnumerable的类都要实现GetEnumerator。可以参看ArrayList,String的GetEnumerator方法。他们都继承自IEnumerable。


参考资料:

1.Dot Net设计模式—工厂方法模式http://fineboy.cnblogs.com/archive/2005/08/04/207459.html

2.工厂方法模式 http://www.cnblogs.com/cbf4life/archive/2009/12/20/1628494.html


LCL_data原创于CSDN.Net【http://blog.csdn.net/lcl_data/article/details/8712834

分享到:
评论

相关推荐

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm” 供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) ...

    设计模式迷你手册.chm

    设计模式迷你手册.chm,大小仅 188 KB,图文并茂,介绍性强,每个设计模式附有 C++、C# 示例源码示例。 目录: 创建型 Factory Method Abstract Factory Builder Prototype Singleton 结构型 Adapter Bridge ...

    设计模式,软件开发者必读

    2.2 FACTORY METHOD工厂方法模式 17 2.2.1 简单工厂 17 2.2.2 工厂方法 18 2.3 ABSTRACT FACTORY 抽象工厂模式 22 2.4 BUILDER PATTERN 生成器模式 25 2.5 PROTOTYPE 原型模式 28 结构型模式 30 3.1 ADAPTER 适配器...

    asp.net知识库

    不该用Generics实现Abstract Factory的理由 C#2.0-泛型 C#2.0-extern C#2.0-可空类型 C#2.0-分部类 C#2.0-迭代器 C#2.0 的新增功能学习 泛型的序列化问题 .NET 2.0 泛型在实际开发中的一次小应用 C#2.0 Singleton 的...

    二十三种设计模式【PDF版】

    设计模式之 Factory(工厂方法和抽象工厂) 使用工厂模式就象使用 new 一样频繁. 设计模式之 Builder 汽车由车轮 方向盘 发动机很多部件组成,同时,将这些部件组装成汽车也是一件复杂的工作,Builder 模式就是将这...

    java 设计模式资料

    附件中是java实现全部的设计模式,包含代码和工程(jbuilder工程),值得收藏. 此目录里包括了一书中所有23种设计模式的实现(Java 版)源码 关于代码的几点说明: 1. 代码为根据个人对Design Pattern的学习理解写...

    深入剖析设计模式中的组合模式应用及在C++中的实现

    组合模式将对象组合成树形结构以表示“部分-整体”的层次结构。...namespace FactoryMethod_DesignPattern { using System; using System.Collections; abstract class Component { protected string strName;

    java 面试题 总结

    声明方法的存在而不去实现它的类被叫做抽象类(abstract class),它用于要创建一个体现某些基本行为的类,并为该类声明方法,但不能在该类中实现该类的情况。不能创建abstract 类的实例。然而可以创建一个变量,其...

    超级有影响力霸气的Java面试题大全文档

    exception 表示一种设计或实现问题。也就是说,它表示如果程序运行正常,从不会发生的情况。 19、同步和异步有何异同,在什么情况下分别使用他们?举例说明。  如果数据将在线程间共享。例如正在写的数据以后可能...

Global site tag (gtag.js) - Google Analytics