#include <iostream>
using namespace std;
template <typename T>
class BaseOper
{
public:
void SetParam(const T& a, const T& b)
{
this->a = a;
this->b = b;
}
virtual T GetResult() = 0;
protected:
T a;
T b;
};
template <typename T>
class AddOper : public BaseOper<T>
{
public:
T GetResult()
{
return a + b;
}
};
template <typename T>
class SubOper : public BaseOper<T>
{
public:
T GetResult()
{
return a - b;
}
};
template <typename T>
class MultiOper : public BaseOper<T>
{
public:
T GetResult()
{
return a * b;
}
};
template <typename T>
class DivOper : public BaseOper<T>
{
public:
T GetResult()
{
return a / b;
}
};
enum emOper{
OP_ADD,
OP_SUB,
OP_MUL,
OP_DIV,
};
template <typename T>
class OperFactory
{
public:
static DivOper<T>* GetOper(int nType)
{
switch(nType)
{
case OP_ADD:
return new AddOper<T>();
break;
case OP_SUB:
return new AddOper<T>();
break;
case OP_MUL:
return new MultiOper<T>();
break;
case OP_DIV:
return new DivOper<T>();
break;
}
}
};
int main(int argc, char *argv[])
{
BaseOper<int> *pAddOp = new AddOper<int>();
pAddOp->SetParam(2,3);
cout << pAddOp->GetResult();
}
simple factory
最新推荐文章于 2022-12-10 17:49:13 发布