缺点:创建类较多
适用:后期扩展较多,或比较复杂
#include<memory>
#include<iostream>
#include<string>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
enum Ctype {coreA,coreB};
class SingleCore{
public:
virtual void print(){
cout << "siglecore_print"<<endl;
}
~SingleCore(){
cout <<"~~singlecore"<<endl;
}
};
class SingleCoreA :public SingleCore{
public:
void print(){
cout << "A_print"<<endl;
}
~SingleCoreA(){
cout <<"~~singlecoreA"<<endl;
}
};
class SingleCoreB :public SingleCore{
public:
void print(){
cout << "B_print"<<endl;
}
~SingleCoreB(){
cout <<"~~singlecoreB"<<endl;
}
};
class Factory{
public:
virtual SingleCore* creatCore(){}
};
class FactoryA : public Factory{
public:
SingleCoreA* creatCore (){
return new SingleCoreA;
}
};
class FactoryB : public Factory{
public:
SingleCoreB* creatCore (){
return new SingleCoreB;
}
};
int main(int argc, char *argv[])
{
auto_ptr<Factory> facA (new FactoryA() );
//facA->creatCore ()->print ();
auto_ptr<SingleCore> singA (facA->creatCore ());
singA->print ();
//auto_ptr<SingleCore> singcore (fac->creatCore (coreA) );
//singcore->print ();
return 0;
}