欢迎访问WDPHP素材源码!今天是2024年04月17日 星期三,熬夜对身体不好,早点休息吧!
您好,游客 [ 马上登录 | 注册帐号 | 微信登录 | QQ登录]
当前位置:首页 > 教程 > 其他教程 > 

C++开放封闭原则实例代码分析
栏目分类:其他教程    发布日期:2023-06-05    浏览次数:401次     收藏

这篇文章主要介绍“C++开放封闭原则实例代码分析”,在日常操作中,相信很多人在C++开放封闭原则实例代码分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++开放封闭原则实例代码分析”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

开放-封闭原则可以做到这样,所谓开放-封闭原则就是指软件实体(类、函数、模块等)应该可以扩展,但是不可以修改,即我们设计这个类的时候就尽量让这个类足够好,写好了就不要去修改了,原来的代码能不动则不动,如果新需求来,我们增加一些类就完事了。面对需求的改变,对程序的改动是通过增加新代码进行的,而不是更改现有的代码,这就是开放-封闭原则的精神所在。

在我们最初编写代码时,假设变化不会发生。当变化发生时,我们将创建抽象来隔离以后发生的同类变化。

2.0版本:

#include<iostream>
using namespace std;
#include<string>
class opeException
{
public:
	void getMessage()
	{
		cout << "您的输入有误!" << endl;
	}
};
//判断一个字符串是不是数字
bool isStringNum(string& s)
{
	bool flag = true;
	for (auto e : s)
		if (!isdigit(e))
		{
			flag = false;
			break;
		}	
	return flag;
}
int main()
{
	string num1 = "0";
	string num2 = "0";
	string ope = " ";
	try
	{
		cout << "请输入左操作数:" << endl;
		cin >> num1;
		if (!isStringNum(num1))
			throw opeException();
		cout << "请输入右操作数:" << endl;
		cin >> num2;
		if (!isStringNum(num2))
			throw opeException();
		cout << "请输入操作符" << endl;
		cin >> ope;
		if (ope != "+" && ope != "-" && ope != "*" && ope != "/")
			throw opeException();
		if (ope == "+")
		{
			cout<< stoi(num1) + stoi(num2)<<endl;
		}
		else if (ope == "-")
		{
			cout << stoi(num1) - stoi(num2) << endl;
		}
		else if (ope == "*")
		{
			cout << stoi(num1) * stoi(num2) << endl;
		}
		else if (ope == "/")
		{
			if (stoi(num2) != 0)
			{
				cout << stoi(num1) / stoi(num2) << endl;
			}
			else
				cout << "除数不能为0" << endl;
		}
	}
	catch (opeException ex)
	{
		ex.getMessage();
	}
	return 0;
}

在计算器2.0版本中,如果我们要增加开平方、平方、立方等运算,需要对代码进行大量修改,这显然不满足开放-封闭原则,可维护性很差,面对这些可能的变化,在4.0版本的代码中将各种具体运算,比如加减乘除分别抽象成为加法类、减法类、乘法类、除法类,这样如果我们需要增加一些运算,面对这些变化,我们只需要再创建相应的运算类即可。

4.0版本:

#include<iostream>
using namespace std;
#include<string>
//业务逻辑
//异常类用于处理异常情况
class opeException
{
public:
	void getMessage()
	{
		cout << "您的输入有误!" << endl;
	}
};
//运算类
class Operation
{	
	//判断一个字符串是不是数字
	bool isStringNum(string& s)
	{
		bool flag = true;
		for (auto e : s)
			if (!(isdigit(e)))
			{
				flag = false;
				break;
			}
		return flag;
	}
protected:
//判断输入的操作数和操作符是否有误
	bool isError(string& _strNum1, string& _strNum2, string& _ope)
	{
		if (!(Operation::isStringNum(_strNum1) && Operation::isStringNum(_strNum2) && (_ope == "+" || _ope == "-" || _ope == "*" || _ope == "/")))
		{
			return false;
		}
	}
public:
	virtual int getResult() = 0;
};
//加法运算类
class addOperation:public Operation
{
private:
	string strNum1;
	string strNum2;
	string ope;
	int re;
public:
	addOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1),strNum2(_strNum2),ope(_ope),re(0) {}
	virtual int getResult() override
	{
		if (!isError(strNum1, strNum2, ope))
			throw opeException();
		else
			re = stoi(strNum1) + stoi(strNum2);
		return re;
	}
};
//减法运算类
class subOperation :public Operation
{
private:
	string strNum1;
	string strNum2;
	string ope;
	int re;
public:
	subOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {}
	virtual int getResult() override
	{
		if (!isError(strNum1, strNum2, ope))
			throw opeException();
		else
			re = stoi(strNum1) - stoi(strNum2);
		return re;
	}
};
//乘法运算类
class mulOperation :public Operation
{
private:
	string strNum1;
	string strNum2;
	string ope;
	int re;
public:
	mulOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {}
	virtual int getResult() override
	{
		if (!isError(strNum1, strNum2, ope))
			throw opeException();
		else
			re = stoi(strNum1) * stoi(strNum2);
		return re;
	}
};
//除法运算类
class divOperation :public Operation
{
private:
	string strNum1;
	string strNum2;
	string ope;
	int re;
public:
	divOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {}
	virtual int getResult() override
	{
		if (!isError(strNum1, strNum2, ope))
			throw opeException();
		else if (stoi(strNum2) != 0)
			re = stoi(strNum1) / stoi(strNum2);
		else
			throw opeException();
		return re;
	}
};
//界面逻辑
int main()
{
	try
	{
		string _strNum1 = " ";
		string _strNum2 = " ";
		string _ope = " ";
		cout << "请输入左操作数:" << endl;
		cin >> _strNum1;
		cout << "请输入右操作数:" << endl;
		cin >> _strNum2;
		cout << "请输入操作符:" << endl;
		cin >> _ope;
		if (_ope == "+")
		{
			addOperation addoperation(_strNum1, _strNum2, _ope);
			cout << addoperation.getResult() << endl;
		}
		else if (_ope == "-")
		{
			subOperation suboperation(_strNum1, _strNum2, _ope);
			cout << suboperation.getResult() << endl;
		}
		else if (_ope == "*")
		{
			mulOperation muloperation(_strNum1, _strNum2, _ope);
			cout << muloperation.getResult() << endl;
		}
		else if (_ope == "/")
		{
			divOperation muloperation(_strNum1, _strNum2, _ope);
			cout << muloperation.getResult() << endl;
		}
		else
			cout << "您的输入有误!" << endl;
	}
	catch (opeException ex)
	{
		cout << "您的输入有误" << endl;
	}
	return 0;
}

当然,并不是什么时候应对变化都是容易的。我们希望的是在开发工作展开不久就知道可能发生的变化。查明可能发生的变化所等待的时间越长,要创建正确的抽象就越困难。比如,如果加减运算都在很多地方应用了,再考虑抽象、考虑分离,就很困难。

开放-封闭原则是面向对象设计的核心所在。遵循这个原则可以带来面向对象技术所声称的巨大好处,也就是可维护、可扩展、可复用、灵活性好。开发人员应该仅对程序中呈现出频繁变化的那些部分做出抽象,然而,对于应用程序中的每个部分都刻意地进行抽象同样不是一个好主意。拒绝不成熟的抽象和抽象本身一样重要。

源码 模板 特效 素材 资源 教程 站长