#include<iostream>
using namespace std;
class Operation{
public:
virtual double getResult(double a,double b) = 0;
};
class Factory{
public:
virtual Operation * create() = 0;
};
class AddOperation : public Operation{
public:
double getResult(double a,double b){return a + b;};
};
class TimesOperation : public Operation{
public:
double getResult(double a,double b){return a * b;};
};
class AddFactory : public Factory{
public:
Operation * create(){return new AddOperation;};
};
class TimesFactory : public Factory{
public:
Operation * create(){return new TimesOperation;};
};
int main(){
Factory * addF = new AddFactory;
Factory * timesF = new TimesFactory;
Operation * add = addF->create();
Operation * times = timesF->create();
cout<<"1 + 2 = "<<add->getResult(1,2)<<endl;
cout<<"1 * 2 = "<<times->getResult(1,2)<<endl;
return 0;
}
工厂方法模式c++实现
最新推荐文章于 2023-06-23 20:31:10 发布