C++设计模式从0进击-3-工厂模式
工厂模式(Factory)
//工厂模式 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; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。