// 4_FactoryMethod_2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
// 雷锋
class LeiFeng
{
public:
void Sweep()
{
cout << "扫地" << endl;
}
void Wash()
{
cout << "洗衣" << endl;
}
void BuyRice()
{
cout << "买米" << endl;
}
};
// 学雷锋的大学生
class Undergraduate : public LeiFeng
{
};
// 社区工作者
class Volunteer : public LeiFeng
{
};
class IFactory
{
public:
LeiFeng* CreateLeiFeng(){ return nullptr; }
};
class UndergraduateFactory : public IFactory
{
public:
LeiFeng* CreateLeiFeng()
{
return new Undergraduate();
}
};
class VolunteerFactory : public IFactory
{
public:
LeiFeng* CreateLeiFeng()
{
return new Volunteer();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
IFactory* factory = new UndergraduateFactory();
LeiFeng* studeng = factory->CreateLeiFeng();
studeng->BuyRice();
studeng->Sweep();
studeng->Wash();
return 0;
}
运行结果: