//工厂模式
class LeiFeng
{
public:
virtual void Sweep()
{
cout << "雷锋扫地" << endl;
}
};
//学雷锋的大学生
class Student: public LeiFeng
{
public:
virtual void Sweep()
{
cout << "大学生扫地" << endl;
}
};
//学雷锋的志愿者
class Volenter: public LeiFeng
{
public:
virtual void Sweep()
{
cout << "志愿者扫地" << endl;
}
};
//工厂基类Creator
class LeiFengFactory{
public:
virtual LeiFeng * CreateLeiFeng()
{
return new LeiFeng();
}
};
//工厂具体类
class StuendFactory: public LeiFengFactory
{
public:
virtual LeiFeng * CreateLeiFeng()
{
return new Student();
}
};
class VolenterFactory: public LeiFengFactory
{
public:
virtual LeiFeng * CreateLeiFeng()
{
return new Volenter();
}
};
int _tmain(int argc, _TCHAR(argv[]))
{
LeiFengFactory * factory = new LeiFengFactory();
LeiFeng * s = factory->CreateLeiFeng();
s->Sweep();
delete s;
delete factory;
return 0;
return 0;
}原文:http://blog.csdn.net/lightupheaven/article/details/38824357