简单工厂设计模式

#include
#include
#include


using namespace std;


struct Operation
{
Operation():_douA(0),_douB(0){};
virtual double getResult(){};
double getA() {return _douA;}
double setA(double A) { _douA = A;}
double getB() {return _douB;}
double setB(double B) { _douB = B;}
private:
double _douA;
double _douB;;
};


struct OperationAdd : Operation
{#include
#include
#include

using namespace std;

struct Operation
{
Operation():_douA(0),_douB(0){};
virtual double getResult(){};
double getA() {return _douA;}
double setA(double A) { _douA = A;}
double getB() {return _douB;}
double setB(double B) { _douB = B;}
private:
double _douA;
double _douB;;
};

struct OperationAdd : Operation
{
double getResult()
{
return getA() + getB();
}
};

struct OperationSub : Operation
{
double getResult()
{
return getA() - getB();
}
};

struct OperationMul : Operation
{
double getResult()
{
return getA() * getB();
}
};

struct OperationDiv : Operation
{
double getResult()
{
if (!getB())
return getA() / getB();
else
return 0;
}
};

/*如果添加新运算,直接多一个if就可以了,封装性强,不用改动任何其他代码。秒不可言*/
struct OperationFactory
{
static Operation* createOperation(const string& op)
{
if (op == "+")
return new OperationAdd;
if (op == "-")
return new OperationSub;
if (op == "*")
return new OperationMul;
if (op == "+")
return new OperationDiv;
return 0;
}
};



template
T string2other(const string& s)
{
istringstream iss(s);
T num;
iss >> num;
return num;
}
int main()
{
cout << "请输入数字A" << endl;
string strNumA;
cin >> strNumA;

cout <<"请输入运算符(+,-,*,/):\r\n" << strNumA;
string op;
cin >> op;

cout << "请输入数字B:\r\n" << strNumA << op ;
string strNumB;
cin >> strNumB;


/*以下代码将不用发生任何改变,就能实现运算。妙不可言*/
Operation* operation = OperationFactory::createOperation(op);
operation->setA(string2other(strNumA));
operation->setB(string2other(strNumB));
cout << operation->getResult() << endl;
}

double getResult()
{
return getA() + getB();
}
};


struct OperationSub : Operation
{
double getResult()
{
return getA() - getB();
}
};


struct OperationMul : Operation
{
double getResult()
{
return getA() * getB();
}
};


struct OperationDiv : Operation
{
double getResult()
{
if (!getB())
return getA() / getB();
else
return 0;
}
};


/*如果添加新运算,直接多一个if就可以了,封装性强,不用改动任何其他代码。秒不可言*/
struct OperationFactory
{
static Operation* createOperation(const string& op)
{
if (op == "+")
return new OperationAdd;
if (op == "-")
return new OperationSub;
if (op == "*")
return new OperationMul;
if (op == "+")
return new OperationDiv;
return 0;
}
};


template
T string2other(const string& s)
{
istringstream iss(s);
T num;
iss >> num;
return num;
}
int main()
{
cout << "请输入数字A" << endl;
string strNumA;
cin >> strNumA;


cout <<"请输入运算符(+,-,*,/):\r\n" << strNumA;
string op;
cin >> op;


cout << "请输入数字B:\r\n" << strNumA << op ;
string strNumB;
cin >> strNumB;




/*以下代码将不用发生任何改变,就能实现运算。妙不可言*/
Operation* operation = OperationFactory::createOperation(op);
operation->setA(string2other(strNumA));
operation->setB(string2other(strNumB));
cout << operation->getResult() << endl;

}


简单工厂设计模式好处:第一:需要什么对象,工厂就制造一个对象。逻辑层代码不用任何改变。

第二:新增对象,不用改其他代码,只需要在工厂中添加即可。封装性号,稳定性好

你可能感兴趣的:(c++)