现在的位置: 首页 > 综合 > 正文

工厂方法模式

2018年04月29日 ⁄ 综合 ⁄ 共 716字 ⁄ 字号 评论关闭
#include <iostream>
using namespace std;

class Leifeng
{
public:
	virtual void sweep()
	{
		cout << "雷锋扫地" << endl;
	}
};

class StudentLeifeng : public Leifeng
{
public:
	virtual void sweep()
	{
		cout << "学生雷锋扫地" << endl;
	}
};

class volunteerLeifeng : public Leifeng
{
public:
	virtual void sweep()
	{
		cout << "志愿者雷锋扫地" << endl;
	}
};

class LeifengFactory
{
public:
	virtual Leifeng* CreateLeifeng()
	{
		return new Leifeng();
	}
};

class StuLeifengFactory : public LeifengFactory
{
public:
	virtual Leifeng* CreateLeifeng() 
	{
		return new StudentLeifeng();
	}
};

class volLeifengFactory : public LeifengFactory
{
	virtual Leifeng* CreateLeifeng()
	{
		return new volunteerLeifeng();
	}
};

int main(void)
{
	//LeifengFactory* aaa = new LeifengFactory();
	StuLeifengFactory* aaa = new StuLeifengFactory();
	Leifeng *p = aaa->CreateLeifeng();
	p->sweep();

	return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.