c++ 工厂模式


#include <iostream>

using namespace std;

class LeiFeng
{
public:
virtual void Sweep()
{
cout<<"扫地"<<endl;
}

virtual void Wash()
{
cout<<"洗衣"<<endl;
}

virtual void BuyRice()
{
cout<<"买米"<<endl;
}

};

class Undergraduate:public LeiFeng
{
public:
void Sweep()
{
cout<<"大学生扫地"<<endl;
}

void Wash()
{
cout<<"大学生洗衣"<<endl;
}

void BuyRice()
{
cout<<"大学生买米"<<endl;
}
};

class Volunter:public LeiFeng
{
public:
void Sweep()
{
cout<<"志愿者扫地"<<endl;
}

void Wash()
{
cout<<"志愿者洗衣"<<endl;
}

void BuyRice()
{
cout<<"志愿者买米"<<endl;
}
};

class IFactory
{
public:
virtual LeiFeng* CreateLeiFeng(){return NULL;};
};


class UndergraduateFactory:public IFactory
{
public:
LeiFeng* CreateLeiFeng() override
{
return new Undergraduate();
}
};

class VolunterFactory:public IFactory
{
public:
LeiFeng* CreateLeiFeng() override
{
return new Volunter();
}
};
int main()
{
IFactory* factory = new VolunterFactory();
LeiFeng* leifeng = factory->CreateLeiFeng();

leifeng->BuyRice();
leifeng->Sweep();
leifeng->Wash();

getchar();

return 0;
}

c++ 工厂模式 - 1307520486 - 1307520486的博客 转发至微博
 
c++ 工厂模式 - 1307520486 - 1307520486的博客 转发至微博
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值