Chapter5:工厂方法模式

先说下简单工厂和工厂方法的区别:

简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。

工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。

工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现产品类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。想要加功能,本来是改工厂类的,而现在是修改客户端。

实际应用

工程结构:

(1)抽象产品类 IFruit.h

(2)抽象工厂类 IFruitGardener.h

(3)具体产品类 CApple.h、CGrape.h、CStrawberry.h

(4)具体工厂类 CAppleGardener.h、CGrapeGardener.h、CStrawberryGardener.h

(5)客户端       FactoryMethodApplication.cpp

 

(1)抽象产品类 IFruit.h

/************************************************************************/
/* 抽象水果类(abstract Product) */
/************************************************************************/
#ifndef _IFRUIT_H_
#define _IFRUIT_H_

#include
<string>
#include
<iostream>
using namespace std;

class IFruit
{
public:
virtual void grow() = 0;
virtual void harvest() = 0;
virtual void plant() = 0;
};

#endif //_IFRUIT_H_

 

 

(2)抽象工厂类 IFruitGardener.h

 

/************************************************************************/
/* 抽象水果园丁类(abstract Factory) */
/************************************************************************/
#ifndef _IFRUIT_GARDENER_H_
#define _IFRUIT_GARDENER_H_

#include
"IFruit.h"

class IFruitGardener
{
public:
virtual IFruit* Factory() = 0;
};

#endif //_IFRUIT_GARDENER_H_

 

 

(3)具体产品类 CApple.h、CGrape.h、CStrawberry.h

 

/************************************************************************/
/* 具体的苹果类(Concrete Product) */
/************************************************************************/
#ifndef _APPLE_H_
#define _APPLE_H_

#include
"IFruit.h"

class CApple : public IFruit
{
public:
void grow()
{
cout
<< "Apple is growing..." << endl;
};

void harvest()
{
cout
<< "Apple has been harvested." << endl;
};

void plant()
{
cout
<< "Apple has been planted." << endl;
};

int GetTreeAge()
{
return m_iAppleTreeAge;
};

void SetTreeAge(const int iAge)
{
m_iAppleTreeAge
= iAge;
}

private:
int m_iAppleTreeAge;
};

#endif //_APPLE_H_

 

/************************************************************************/
/* 具体的葡萄类(Concrete Product) */
/************************************************************************/
#ifndef _GRAPE_H_
#define _GRAPE_H_

#include
"IFruit.h"

class CGrape : public IFruit
{
public:
void grow()
{
cout
<< "Grape is growing..." << endl;
};

void harvest()
{
cout
<< "Grape has been harvested." << endl;
};

void plant()
{
cout
<< "Grape has been planted." << endl;
};

bool GetSeedless()
{
return m_bSeedless;
};

void SetSeedless(const bool bSeedless)
{
m_bSeedless
= bSeedless;
};

private:
bool m_bSeedless;
};

#endif //_GRAPE_H_

 

/************************************************************************/
/* 具体的草莓类(Concrete Product) */
/************************************************************************/
#ifndef _STRAWBERRY_H_
#define _STRAWBERRY_H_

#include
"IFruit.h"

class CStrawberry : public IFruit
{
public:
void grow()
{
cout
<< "Strawberry is growing..." << endl;
};

void harvest()
{
cout
<< "Strawberry has been harvested." << endl;
};

void plant()
{
cout
<< "Strawberry has been planted." << endl;
};
};

#endif //_STRAWBERRY_H_

 

 

(4)具体工厂类 CAppleGardener.h、CGrapeGardener.h、CStrawberryGardener.h

 

/************************************************************************/
/* 具体的苹果园丁类(Concrete Factory) */
/************************************************************************/
#ifndef _APPLE_GARDENER_H_
#define _APPLE_GARDENER_H_

#include
"IFruitGardener.h"
#include
"CApple.h"

class CAppleGardener : public IFruitGardener
{
public:
CAppleGardener():m_pApple(NULL){};

IFruit
* Factory()
{
if (NULL == m_pApple)
{
m_pApple
= new CApple();
}

return m_pApple;
};

private:
CApple
* m_pApple;
};

#endif //_APPLE_GARDENER_H_

 

/************************************************************************/
/* 具体的葡萄园丁类(Concrete Factory) */
/************************************************************************/
#ifndef _GRAPE_GARDENER_H_
#define _GRAPE_GARDENER_H_

#include
"IFruitGardener.h"
#include
"CGrape.h"

class CGrapeGardener : public IFruitGardener
{
public:
CGrapeGardener():m_pGrape(NULL){};

IFruit
* Factory()
{
if (NULL == m_pGrape)
{
m_pGrape
= new CGrape();
}

return m_pGrape;
};

private:
CGrape
* m_pGrape;
};

#endif //_GRAPE_GARDENER_H_

 

/************************************************************************/
/* 具体的草莓园丁类(Concrete Factory) */
/************************************************************************/
#ifndef _STRAWBERRY_GARDENER_H_
#define _STRAWBERRY_GARDENER_H_

#include
"IFruitGardener.h"
#include
"CStrawberry.h"

class CStrawberryGardener : public IFruitGardener
{
public:
CStrawberryGardener():m_pStrawberry(NULL){};

IFruit
* Factory()
{
if (NULL == m_pStrawberry)
{
m_pStrawberry
= new CStrawberry();
}

return m_pStrawberry;
};

private:
CStrawberry
* m_pStrawberry;
};

#endif //_STRAWBERRY_GARDENER_H_

 

 

(5)客户端       FactoryMethodApplication.cpp

 

// FactoryMethodApplication.cpp : 定义控制台应用程序的入口点。
//

#include
"stdafx.h"
#include
<Windows.h>
#include
"IFruitGardener.h"
#include
"CAppleGardener.h"
#include
"CGrapeGardener.h"
#include
"CStrawberryGardener.h"

int _tmain(int argc, _TCHAR* argv[])
{
static IFruitGardener* pFruitFactory1 = NULL;
static IFruitGardener* pFruitFactory2 = NULL;
static IFruit* pFruit1 = NULL;
static IFruit* pFruit2 = NULL;

pFruitFactory1
= new CAppleGardener();
if (NULL != pFruitFactory1)
{
pFruit1
= pFruitFactory1->Factory();
if (NULL != pFruit1)
{
pFruit1
->grow();
pFruit1
->harvest();
pFruit1
->plant();
}
}

pFruitFactory2
= new CGrapeGardener();
if (NULL != pFruitFactory2)
{
pFruit2
= pFruitFactory2->Factory();
if (NULL != pFruit2)
{
pFruit2
->grow();
pFruit2
->harvest();
pFruit2
->plant();
}
}

Sleep(
10000);

return 0;
}

 

 

posted @ 2010-08-12 21:12  奋斗の机器  阅读(263)  评论(0编辑  收藏  举报