0
点赞
收藏
分享

微信扫一扫

1.工厂模式


面向对象得编程思想:通过封装、继承、多态把程序的耦合度降低,用设计模式使得程序更加的灵活、容易修改并且易于复用;

复用:让业务逻辑和界面逻辑分开,让它们之间的耦合度下降,只有这样才能达到易于维护或扩展。

继承和多态:降低耦合性,让原有已经编译好的程序不参与功能添加中,增加新功能不需要原有算法运算类参与。

版本1:功能实现

版本1:功能实现
class Program {
public:
	double str2double(char* str) {
		return atof(str);
	}
	void cal(void) {
		try {
			cout << "请输入数字A: " << endl;
			cin >> strNumberA;
			cout << "请选择运算符号(+、-、*、\) " << endl;
			cin >> strOperate;
			cout << "请输入数字B: " << endl;
			cin >> strNumberB;
			switch (strOperate)
			{
			case '+':
				result = str2double(&strNumberA[0]) + str2double(&strNumberB[0]); break;
			case '-':
				result = str2double(&strNumberA[0]) - str2double(&strNumberB[0]); break;
			case '*':
				result = str2double(&strNumberA[0]) * str2double(&strNumberB[0]); break;
			case '/':
				if (strNumberB == "0") {
					throw runtime_error("除数不能为0");
				}
				result = str2double(&strNumberA[0]) / str2double(&strNumberB[0]); break;
			default:
				break;
			}
			cout << "结果是: " << result <<endl;
		}
		catch (runtime_error err) {
			cout << err.what() << endl;
		}
	}
private:
	string strNumberA, strNumberB;
	double result;
	char strOperate;
};

int main(int argc,char** argv)
{
	Program myprogram;
	myprogram.cal();
	return 0;
}

版本2:业务封装

让业务逻辑和界面逻辑分开

class Operation {

public:

       static double GetResult(double NumberA, double NumberB,char strOperate){

                     double result=0.0f;

                     switch (strOperate)

                     {

                     case '+':

                            result = NumberA + NumberB; break;

                     case '-':

                            result = NumberA - NumberB; break;

                     case '*':

                            result = NumberA * NumberB; break;

                     case '/':

                            result = NumberA / NumberB; break;

                     default:

                            break;

                     }

                     return result;

       }

};



class Program {

public:

       double str2double(char* str) {

              return atof(str);

       }

       void cal(void) {

              try {

                     cout << "请输入数字A: " << endl;

                     cin >> strNumberA;

                     cout << "请选择运算符号(+、-、*、\) " << endl;

                     cin >> strOperate;

                     cout << "请输入数字B: " << endl;

                     cin >> strNumberB;

                     if (strNumberB == "0" && strOperate=='/') {

                            throw runtime_error("除数不能为0");

                     }

                     result = Operation::GetResult(str2double(&strNumberA[0]), str2double(&strNumberB[0]), strOperate);

                     cout << "结果是: " << result << endl;

              }

              catch (runtime_error err) {

                     cout << err.what() << endl;

              }

       }

private:

       string strNumberA, strNumberB;

       double result;

       char strOperate;

};



int main(int argc,char** argv)

{

       Program myprogram;

       myprogram.cal();

       return 0;

}

版本3:低耦合

继承和多态:若要添加新功能(平方根、立方根、自然对数、正余弦)只需要增加相应的运算子类,同时修改运算类工厂。

class Operation {

public:

         virtual double GetResult() {

                  double result = 0.0f;

                  return result;

         }

         double getA() {

                  return NumberA;

         }

         double getB() {

                  return NumberB;

         }

         void setA(double var) {

                  NumberA = var;

         }

         void setB(double var) {

                  NumberB = var;

         }

protected:

         double NumberA;

         double NumberB;

};



class OperationAdd:public Operation {

public: // 加法运算类

         double GetResult() {

                  return NumberA + NumberB;

         }

};

class OperationSub :public Operation {

public: // 减法运算类

         double GetResult() {

                  return NumberA - NumberB;

         }

};

class OperationMul :public Operation {

public: // 乘法运算类

         double GetResult() {

                  return NumberA * NumberB;

         }

};

class OperationDiv :public Operation {

public: // 除法运算类

         double GetResult() {

                  try {

                          if (NumberB == 0)

                                   throw runtime_error("除数不能为0");

                          return NumberA / NumberB;

                  }

                  catch (runtime_error err) {

                          cout << err.what() << endl;

                  }

                  return 0;

         }

};



class OperationFactory {

public: // 运算类工厂

         static Operation* createOperate(char operate) {

                  Operation *oper=NULL;

                  switch (operate)

                  {

                          case '+':

                                   oper = new OperationAdd; break;

                          case '-':

                                   oper = new OperationSub; break;

                          case '*':

                                   oper = new OperationMul; break;

                          case '/':

                                   oper = new OperationDiv; break;

                          default:

                                   break;

                  }

                  return oper;

         }

};

class Program {

public:

         double str2double(char* str) {

                  return atof(str);

         }

         void cal(void) {

                  try {

                          cout << "请输入数字A: " << endl;

                          cin >> strNumberA;

                          cout << "请选择运算符号(+、-、*、\) " << endl;

                          cin >> strOperate;

                          cout << "请输入数字B: " << endl;

                          cin >> strNumberB;

                          if (strNumberB == "0" && strOperate=='/') {

                                   throw runtime_error("除数不能为0");

                          }

                  //       result = Operation::GetResult(str2double(&strNumberA[0]), str2double(&strNumberB[0]), strOperate);

                          cout << "结果是: " << result << endl;

                  }

                  catch (runtime_error err) {

                          cout << err.what() << endl;

                  }

         }

private:

         string strNumberA, strNumberB;

         double result;

         char strOperate;

};



int main(int argc,char** argv)

{

         Operation *oper=NULL;

         oper = OperationFactory::createOperate('/');

         oper->setA(10);

         oper->setB(20);

         double result = oper->GetResult();

         return 0;

}

 

举报

相关推荐

0 条评论