当前位置: 首页 > news >正文

汕尾商城网站建设泰州东方医院男科

汕尾商城网站建设,泰州东方医院男科,wordpress随机文章列表,云南省网站建设单一职责原则: 就一个类而言,应该只有一个引起它变化的原因,如果一个类承担的职责过多就等于把这些职责耦合在一起,至少会造成以下两方面的问题: 我们要去修改该类中的一个职责可能会影响到该类的其它职责。这种耦合…

单一职责原则:

就一个类而言,应该只有一个引起它变化的原因,如果一个类承担的职责过多就等于把这些职责耦合在一起,至少会造成以下两方面的问题:

  • 我们要去修改该类中的一个职责可能会影响到该类的其它职责。这种耦合会导致脆弱的设计,当变化发生时,设计会遭受到意想不到的破坏。
  • 当客户端仅需要该对象的某一个职责时,不得不将其他不需要的职责全都包含进来,从而造成冗余代码或代码的浪费。

我们在设计一个类时要学会发现职责,并把那些职责相互分离,其实要去判断是否应该分离出一个类来并不难,前面说过,一个类应该只有一个引起它变化的原因,如果你能想到其它的原因也能去改变这个类,那么这个类就具有多于1个的职责,就应该考虑类的职责分离。

在之前的这篇博客中,https://blog.csdn.net/weixin_44049823/article/details/128907849,我们实现的计算器实际上也用到了单一职责原则,这里我们选出其中最经典的3.0版本和5.0版本来学习单一职责原则。

3.0版本计算器代码如下:

#include<iostream>
using namespace std;
#include<string>//业务逻辑
//异常类用于处理异常情况
class opeException
{
public:void getMessage(){cout << "您的输入有误!" << endl;}
};//运算类用于处理运算
class Operation
{
public:Operation(string& _num1, string& _num2, string& _ope) :num1(_num1), num2(_num2), ope(_ope){}//获取运算结果int getResult(){if (!(isStringNum(num1) && isStringNum(num2) && (ope == "+" || ope == "-" || ope == "*" || ope == "/")))throw opeException();if (ope == "+"){re = stoi(num1) + stoi(num2);}else if (ope == "-"){re = stoi(num1) - stoi(num2);}else if (ope == "*"){re = stoi(num1) * stoi(num2);}else if (ope == "/"){if (stoi(num2) != 0){re = stoi(num1) / stoi(num2);}elsethrow opeException();}return re;}
private:int re;string num1;string num2;string ope;//判断一个字符串是不是数字bool isStringNum(string& s){bool flag = true;for (auto e : s)if (!(isdigit(e))){flag = false;break;}return flag;}
};//界面逻辑
int main()
{try{string _num1 = " ";string _num2 = " ";string _ope = " ";cout << "请输入左操作数:" << endl;cin >> _num1;cout << "请输入右操作数:" << endl;cin >> _num2;cout << "请输入操作符" << endl;cin >> _ope;Operation operation(_num1, _num2, _ope);cout << operation.getResult() << endl;}catch (opeException &ex){ex.getMessage();}return 0;
}

仅仅一个运算类Operation就实现了加减乘除4种功能,很明显在这个类中我至少有4个原因去修改这个类,我修改加法算法的时候可能会影响到其它的运算算法,这个类的耦合太高且严重违反了单一职责原则。

修改后的5.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();elsere = 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();elsere = 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();elsere = 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);elsethrow opeException();return re;}
};//运算工厂类
class OpeFactory
{
public:Operation& choose(string &_strNum1,string &_strNum2,string &_ope){if (_ope == "+"){operation = new addOperation(_strNum1, _strNum2, _ope);}else if (_ope == "-")operation = new subOperation(_strNum1, _strNum2, _ope);else if (_ope == "*")operation = new mulOperation(_strNum1, _strNum2, _ope);else if (_ope == "/"){operation = new divOperation(_strNum1, _strNum2, _ope);}elseoperation = nullptr;return *operation;}private:Operation* operation;
};//界面逻辑
int main()
{try{string _strNum1 = " ";string _strNum2 = " ";string _ope = " ";cout << "请输入左操作数:" << endl;cin >> _strNum1;cout << "请输入右操作数:" << endl;cin >> _strNum2;cout << "请输入操作符:" << endl;cin >> _ope;OpeFactory factory;Operation* re = &factory.choose(_strNum1, _strNum2, _ope);if (re != nullptr)cout << (*re).getResult() << endl;elsecout << "您的输入有误!" << endl;}catch (opeException ex){cout << "您的输入有误" << endl;}return 0;
}

在5.0版本的计算器代码中,我们将运算类分成了4种类,分别是加法类、减法类、乘法类、除法类,还创建了一个工厂类专门用于根据不同情况实例化对象,每个类只有一个职责,我们要修改某个功能只需要去修改对应的类即可,极大降低了代码之间的耦合。

单一职责原则的核心就是控制类的粒度大小、将对象解耦、提高其内聚性。如果遵循单一职责原则将有以下优点:

  • 降低类的复杂度。一个类只负责一项职责,其逻辑肯定要比负责多项职责简单得多。
  • 提高类的可读性。复杂性降低,自然其可读性会提高。
  • 提高系统的可维护性。可读性提高,那自然更容易维护了。
  • 变更引起的风险降低。变更是必然的,如果单一职责原则遵守得好,当修改一个功能时,可以显著降低对其他功能的影响。
http://www.yayakq.cn/news/42344/

相关文章:

  • 关于网站开发的一些论文成都程序员网站
  • 建设银行网站个人客户建设网站要钱吗
  • 小勐拉网站建设临沂百度公司地址
  • 开办时 网站建设费 科目长沙网站推广 下拉通推广
  • 产品ui设计公司湛江市seo网站设计哪里好
  • 公司做网站需要注意什么事情平面设计公司起名
  • 营销手段和技巧专业搜索引擎seo技术公司
  • 简单网站建设视频教程百度提交入口网址是指在哪里
  • wordpress怎么静态页面湛江网站seo
  • 潍坊建设局职称公布网站拖曳式网站建设
  • 网站 优化前端网站开发兼职
  • 网站建设在线培训论述网站建设引言
  • 上海企业网站建设公完整个人网站开发案例
  • wordpress连接ossgoogle 优化推广
  • dede制作的网站挂马给公司做网站需要什么信息
  • 建设银行车主卡网上交罚款网站新闻头条最新消息摘抄
  • 权威的合肥网站推广如何自己开发微信小程序
  • 汉中商城网站建设注册公司核名查询系统
  • 网站建设公司盈利分析免费下载建筑图纸的网站
  • 金山做网站的公司太原网站建设外包价格
  • 网站的图文链接怎么做网站开发常用字体
  • 泰安公司网站建设精品服装网站建设
  • 深做网站公司企业网站 自助建站
  • 滨州网站建设腾度凡科网怎么建网站
  • 网站建设商标属于哪个类别电商网站建设如何
  • 上海响应式网站建设企业新广告法 做网站的
  • 网站建设厘金手指排名二二自己怎么做优惠券网站
  • 河北省建设机械协会官方网站首页华为建站模板
  • 做网站能用思源黑体吗企业网络营销方案策划
  • 广州房地产网站建设公众号引流推广平台