设计模式C++学习笔记之五(Factory Method工厂方法模式)
时间:2011-04-11 来源:wang_gary
工厂方法模式的意义是定义一个创建产品对象的工厂接口,将实际创建工作推迟到子类当中。核心工厂类不再负责产品的创建,这样核心类成为一个抽象工厂角色,仅负责具体工厂子类必须实现的接口,这样进一步抽象化的好处是使得工厂方法模式可以使系统在不修改具体工厂角色的情况下引进新的产品。这个例子更详细的内容及说明可以参考原作者博客:cbf4life.cnblogs.com。贴代码了。
5.1.解释
main(),女娲
IHuman,产品接口
CYellowHuman,产品之一
CWhiteHuman,产品之二
CBlackHuman,产品之三
IHumanFactory,工厂接口
CYellowHumanFactory,工厂之一
CWhiteHumanFactory,工厂之二
CBlackHumanFactory,工厂之三
看代码:
//IHuman.h
#pragma once
class IHuman
{
public:
IHuman(void)
{
}
virtual ~IHuman(void)
{
}
virtual void Laugh() = 0;
virtual void Cry() = 0;
virtual void Talk() = 0;
};
//YellowHuman.h
#pragma once
#include "ihuman.h"
class CYellowHuman :
public IHuman
{
public:
CYellowHuman(void);
~CYellowHuman(void);
void Laugh();
void Cry();
void Talk();
};
//YellowHuman.cpp
#include "StdAfx.h"
#include "YellowHuman.h"
#include <iostream>
using std::cout;
using std::endl;
CYellowHuman::CYellowHuman(void)
{
}
CYellowHuman::~CYellowHuman(void)
{
}
void CYellowHuman::Cry()
{
cout << "黄色人种会哭" << endl;
}
void CYellowHuman::Laugh()
{
cout << "黄色人种会大笑,幸福呀!" << endl;
}
void CYellowHuman::Talk()
{
cout << "黄色人种会说话,一般说的都是双字节" << endl;
}
//WhiteHuman.h
#pragma once
#include "ihuman.h"
class CWhiteHuman :
public IHuman
{
public:
CWhiteHuman(void);
~CWhiteHuman(void);
void Laugh();
void Cry();
void Talk();
};
//WhiteHuman.cpp
#include "StdAfx.h"
#include "WhiteHuman.h"
#include <iostream>
using std::cout;
using std::endl;
CWhiteHuman::CWhiteHuman(void)
{
}
CWhiteHuman::~CWhiteHuman(void)
{
}
void CWhiteHuman::Cry()
{
cout << "白色人种会哭" << endl;
}
void CWhiteHuman::Laugh()
{
cout << "白色人种会大笑,侵略的笑声" << endl;
}
void CWhiteHuman::Talk()
{
cout << "白色人种会说话,一般都是单字节" << endl;
}
//BlackHuman.h
#pragma once
#include "ihuman.h"
class CBlackHuman :
public IHuman
{
public:
CBlackHuman(void);
~CBlackHuman(void);
void Laugh();
void Cry();
void Talk();
};
//BlackHuman.cpp
#include "StdAfx.h"
#include "BlackHuman.h"
#include <iostream>
using std::cout;
using std::endl;
CBlackHuman::CBlackHuman(void)
{
}
CBlackHuman::~CBlackHuman(void)
{
}
void CBlackHuman::Cry()
{
cout << "黑人会哭" << endl;
}
void CBlackHuman::Laugh()
{
cout << "黑人会笑" << endl;
}
void CBlackHuman::Talk()
{
cout << "黑人可以说话,一般人听不懂" << endl;
}
//IHumanFactory.h
#pragma once
#include "IHuman.h"
class IHumanFactory
{
public:
IHumanFactory(void)
{
}
virtual ~IHumanFactory(void)
{
}
virtual IHuman * CreateHuman() = 0;
};
//YellowHuman.h
#pragma once
#include "ihumanfactory.h"
class CYellowHumanFactory :
public IHumanFactory
{
public:
CYellowHumanFactory(void);
~CYellowHumanFactory(void);
virtual IHuman * CreateHuman(void);
};
//YellowHumanFactory.cpp
#include "StdAfx.h"
#include "YellowHumanFactory.h"
#include "YellowHuman.h"
CYellowHumanFactory::CYellowHumanFactory(void)
{
}
CYellowHumanFactory::~CYellowHumanFactory(void)
{
}
IHuman * CYellowHumanFactory::CreateHuman( void )
{
return new CYellowHuman();
}
//WhiteHuman.h
#pragma once
#include "ihumanfactory.h"
class CWhiteHumanFactory :
public IHumanFactory
{
public:
CWhiteHumanFactory(void);
~CWhiteHumanFactory(void);
virtual IHuman * CreateHuman(void);
};
//WhiteHumanFactory.cpp
#include "StdAfx.h"
#include "WhiteHumanFactory.h"
#include "WhiteHuman.h"
CWhiteHumanFactory::CWhiteHumanFactory(void)
{
}
CWhiteHumanFactory::~CWhiteHumanFactory(void)
{
}
IHuman * CWhiteHumanFactory::CreateHuman( void )
{
return new CWhiteHuman();
}
//BlackHuman.h
#pragma once
#include "ihumanfactory.h"
class CBlackHumanFactory :
public IHumanFactory
{
public:
CBlackHumanFactory(void);
~CBlackHumanFactory(void);
virtual IHuman * CreateHuman();
};
//BlackHumanFactory.cpp
#include "StdAfx.h"
#include "BlackHumanFactory.h"
#include "BlackHuman.h"
CBlackHumanFactory::CBlackHumanFactory(void)
{
}
CBlackHumanFactory::~CBlackHumanFactory(void)
{
}
IHuman * CBlackHumanFactory::CreateHuman()
{
return new CBlackHuman();
}
//FactoryMethod.cpp
// FactoryMethod.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "IHuman.h"
#include "YellowHuman.h"
#include "WhiteHuman.h"
#include "BlackHuman.h"
#include "SimpleHumanFactory.h"
#include "StandardHumanFactory.h"
#include "IHumanFactory.h"
#include "YellowHumanFactory.h"
#include "WhiteHumanFactory.h"
#include "BlackHumanFactory.h"
#include <iostream>
using std::cout;
using std::endl;
using std::string;
void DoFactoryMethod1()
{
cout << "----------第一批人是这样的:黄种人工厂来生产黄种人" << endl;
IHumanFactory *pHumanFactory = new CYellowHumanFactory();
IHuman *pHuman = pHumanFactory->CreateHuman();
pHuman->Cry();
pHuman->Laugh();
pHuman->Talk();
delete pHuman;
delete pHumanFactory;
}
void DoFactoryMethod2()
{
cout << "----------第二批人是这样的:白种人工厂来生产白种人" << endl;
IHumanFactory *pHumanFactory = new CWhiteHumanFactory();
IHuman *pHuman = pHumanFactory->CreateHuman();
pHuman->Cry();
pHuman->Laugh();
pHuman->Talk();
delete pHuman;
delete pHumanFactory;
}
void DoFactoryMethod3()
{
cout << "----------第一批人是这样的:黑种人工厂来生产黑种人" << endl;
IHumanFactory *pHumanFactory = new CBlackHumanFactory();
IHuman *pHuman = pHumanFactory->CreateHuman();
pHuman->Cry();
pHuman->Laugh();
pHuman->Talk();
delete pHuman;
delete pHumanFactory;
}
int _tmain(int argc, _TCHAR* argv[])
{
//工厂方法
cout << "----------工厂方法:" << endl;
DoFactoryMethod1();
DoFactoryMethod2();
DoFactoryMethod3();
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
_CrtDumpMemoryLeaks();
return 0;
}
工厂方法属于创建型模式,适合于产品不太多的情况。产品如果很多,则需要用抽象工厂来实现。