简单工厂,工厂模式,抽象工厂
都属于设计模式中的创建型模型,主要功能,帮助把对象的实例化部分抽取出来,增强系统扩展性.
1.简单工厂
简单工厂模式的工厂类一般是使用静态方法,通过接收的参数的不同来返回不同的对象实例。
不修改代码的话,是无法扩展的。
#include<iostream>
usingnamespace std;
class CPerson
{
public:
virtual void Function()
{}
virtual ~CPerson(){}
};
class CWhitePerson : public CPerson
{
public:
virtual void Function()
{
cout<<"WhitePerson!"<<endl;
}
};
class CBlackPerson : public CPerson
{
public:
virtual void Function()
{
cout<<"BlackPerson!"<<endl;
}
};
class CYellowPerson : public CPerson
{
public:
virtual void Function()
{
cout<<"YellowPerson!"<<endl;
}
};
class CFactory
{
public:
static CPerson* CreatPerson(int type)
{
switch(type){
case 0:
return new CWhitePerson();
break;
case 1:
return new CBlackPerson();
case 2:
return new CYellowPerson();
default:
return new CWhitePerson();
break;
}
}
// virtualCPerson*CreatPerson()
// {}
virtual~CFactory(){}
};
intmain()
{
cout<<"HelloWorld!"<<endl;
CPerson*person=CFactory::CreatPerson(0);
person->Function();
person=CFactory::CreatPerson(2);
person->Function();
delete person;
return0;
}
2.工厂模式
工厂方法是针对每一种产品提供一个工厂类。通过不同的工厂实例来创建不同的产品实例。
在同一等级结构中,支持增加任意产品。
#include<iostream>
usingnamespacestd;
classCPerson
{
public:
virtualvoidFunction()
{}
virtual~CPerson(){}
};
classCWhitePerson:publicCPerson
{
public:
virtualvoidFunction()
{
cout<<"WhitePerson!"<<endl;
}
};
classCBlackPerson:publicCPerson
{
public:
virtualvoidFunction()
{
cout<<"BlackPerson!"<<endl;
}
};
classCYellowPerson:publicCPerson
{
public:
virtualvoidFunction()
{
cout<<"YellowPerson!"<<endl;
}
};
classCFactory
{
public:
virtualCPerson*CreatPerson()
{}
virtual~CFactory(){}
};
classCWhitePersonFactory:publicCFactory
{
public:
virtualCPerson*CreatPerson()
{
returnnewCWhitePerson();
}
};
classCBlackPersonFactory:publicCFactory
{
public:
virtualCPerson*CreatPerson()
{
returnnewCBlackPerson();
}
};
classCYellowPersonFactory:publicCFactory
{
public:
virtualCPerson*CreatPerson()
{
returnnewCYellowPerson();
}
};
intmain()
{
cout<<"HelloWorld!"<<endl;
CFactory*factory=newCBlackPersonFactory();
CPerson*person=factory->CreatPerson();
person->Function();
factory=newCWhitePersonFactory;
person=factory->CreatPerson();
person->Function();
delete person;
delete factory;
return0;
}
3.抽象工厂模式
抽象工厂是应对产品族概念的。比如说,每个汽车公司可能要同时生产轿车,货车,客车,那么每一个工厂都要有创建轿车,货车和客车的方法。
应对产品族概念而生,增加新的产品线很容易,但是无法增加新的产品。
#include<iostream>
usingnamespacestd;
classCMale
{
public:
virtualvoidFunction()
{}
virtual~CMale(){}
};
classCFemale
{
public:
virtualvoidFunction()
{}
virtual~CFemale(){}
};
classCWhiteMale:publicCMale
{
public:
virtualvoidFunction()
{
cout<<"WhiteMalePerson!"<<endl;
}
};
classCBlackMale:publicCMale
{
public:
virtualvoidFunction()
{
cout<<"BlackCMalePerson!"<<endl;
}
};
classCYellowMale:publicCMale
{
public:
virtualvoidFunction()
{
cout<<"YellowCMalePerson!"<<endl;
}
};
classCWhiteFemale:publicCFemale
{
public:
virtualvoidFunction()
{
cout<<"WhiteFemalePerson!"<<endl;
}
};
classCBlackFemale:publicCFemale
{
public:
virtualvoidFunction()
{
cout<<"BlackFemalePerson!"<<endl;
}
};
classCYellowFemale:publicCFemale
{
public:
virtualvoidFunction()
{
cout<<"YellowFemalePerson!"<<endl;
}
};
classCFactory
{
public:
virtualCMale*MalePerson(){}
virtualCFemale*FemalePerson(){}
virtual~CFactory(){}
};
classCWhiteFactory:publicCFactory
{
public:
virtualCWhiteMale*MalePerson()
{
returnnewCWhiteMale();
}
virtualCWhiteFemale*FemalePerson()
{
returnnewCWhiteFemale();
}
};
classCBlackFactory:publicCFactory
{
public:
virtualCBlackMale*MalePerson()
{
returnnewCBlackMale();
}
virtualCBlackFemale*FemalePerson()
{
returnnewCBlackFemale();
}
};
classCYellowFactory:publicCFactory
{
public:
virtualCYellowMale*MalePerson()
{
returnnewCYellowMale();
}
virtualCYellowFemale*FemalePerson()
{
returnnewCYellowFemale();
}
};
int main()
{
cout<<"HelloWorld!"<<endl;
CFactory*factory=newCYellowFactory();
CFemale*yellowfemale=factory->FemalePerson();
yellowfemale->Function();
return0;
}
使用工厂模式,返回的实例一定是工厂创建的,而不是从其他对象中获取的。
工厂模式返回的实例可以不是新创建的,返回由工厂创建好的实例也是可以的。
区别
简单工厂:用来生产同一等级结构中的任意产品。(对于增加新的产品,需要修改代码)
工厂模式:用来生产同一等级结构中的固定产品。(支持增加任意产品)
抽象工厂:用来生产不同产品族的全部产品。(对于增加新的产品,无能为力;支持增加产品族.抽象工厂里一个工厂要生产多个产品,当抽象工厂生产的产品只有一个时,就叫工厂模式了