开放-封闭原则

我们在做任何系统的时候,都不要指望系统一开始就完全确定需求,然后再也不发生变化,这是不现实,也是不科学的想法,既然需求是一定会发生变化的,那么如何在面对需求的变化时,设计的软件可以相对容易修改,不至于说,新需求一来就要把整个程序都推倒重来呢?

开放-封闭原则可以做到这样,所谓开放-封闭原则就是指软件实体(类、函数、模块等)应该可以扩展,但是不可以修改,即面对需求的改变,对程序的改动是通过增加新代码进行的,而不是更改现有的代码,这就是开放-封闭原则的精神所在。在设计一个类时,我们需要观察到一个类可能发生的变化,并且将这种变化抽象出来,然后具体的变化去继承抽象出来的变化。

在之前的这篇博客中,https://blog.csdn.net/weixin_44049823/article/details/128907849,我们实现了计算器的5个版本,这其中就运用了开放-封闭原则,这里,我们通过该篇博客实现的2.0版本和4.0版本来学习开放-封闭原则。

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版本:

#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;
}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

子非鱼Swx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值