#include<iostream>
using namespace std;
只需关心产品的接口,只要接口保持不变,系统中的上层模块就不会发生变化。
2.典型的解耦框架。高层模块只需要知道产品的抽象类。
3.客户代码可以做到与特定应用无关,适用于任何实体类。
*/
class IProduct{
public:
virtual void print()=0;
};
class ProductA :public IProduct{
public:
void print(){
cout<<"productA"<<endl;
}
};
class ProductB :public IProduct{
public:
void print(){
cout<<"productB"<<endl;
}
};
class IFactory{
public:
virtual IProduct* getProduct()=0;
};
class FectoryA :public IFactory{
public:
IProduct* getProduct(){
return new ProductA();
}
};
class FectoryB:public IFactory{
public:
IProduct* getProduct(){
return new ProductB();
}
};
int main()
{
FectoryA fectoryA;
IProduct * productA= fectoryA.getProduct();
productA->print();
FectoryB fectoryB;
IProduct * productB=fectoryB.getProduct();
productB->print();
delete productA;
delete productB;
return 0;
}
using namespace std;
/*
工厂方法:
只需关心产品的接口,只要接口保持不变,系统中的上层模块就不会发生变化。
2.典型的解耦框架。高层模块只需要知道产品的抽象类。
3.客户代码可以做到与特定应用无关,适用于任何实体类。
*/
class IProduct{
public:
virtual void print()=0;
};
class ProductA :public IProduct{
public:
void print(){
cout<<"productA"<<endl;
}
};
class ProductB :public IProduct{
public:
void print(){
cout<<"productB"<<endl;
}
};
class IFactory{
public:
virtual IProduct* getProduct()=0;
};
class FectoryA :public IFactory{
public:
IProduct* getProduct(){
return new ProductA();
}
};
class FectoryB:public IFactory{
public:
IProduct* getProduct(){
return new ProductB();
}
};
int main()
{
FectoryA fectoryA;
IProduct * productA= fectoryA.getProduct();
productA->print();
FectoryB fectoryB;
IProduct * productB=fectoryB.getProduct();
productB->print();
delete productA;
delete productB;
return 0;
}