类图如下:
代码实现如下:最近时间比较忙,后期考虑上传到github上。
#include<iostream>
#include<string>
using namespace std;
class Operation{
public:
virtual double getResult()=0;
void SetNumberA(double value){
NumberA = value;
}
void SetNumberB(double value){
NumberB = value;
}
protected:
double NumberA;
double NumberB;
};
class abstractFatory{
public:
virtual Operation* createOperation()=0;
};
class addOperation:public Operation{
public:
double getResult(){
return NumberA+NumberB;
}
};
class addFactory:public abstractFatory{
public:
Operation* createOperation(){
return new addOperation;
}
};
class subOperation:public Operation{
public:
double getResult(){
return NumberA-NumberB;
}
};
class subFactory:public abstractFatory{
public:
Operation* createOperation(){
return new subOperation;
}
};
class mulOperation:public Operation{
public:
double getResult(){
return NumberA*NumberB;
}
};
class mulFactory:public abstractFatory{
public:
Operation* createOperation(){
return new mulOperation;
}
};
class divOperation:public Operation{
public:
double getResult(){
return NumberA/NumberB;
}
};
class divFactory:public abstractFatory{
public:
Operation* createOperation(){
return new divOperation;
}
};
int main(){
abstractFatory* factory=new divFactory;
Operation* operation=factory->createOperation();
operation->SetNumberA(16);
operation->SetNumberB(8);
std::cout<<operation->getResult()<<std::endl;
}
?
|