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

厦门广告公司网站建设最新版wordpress

厦门广告公司网站建设,最新版wordpress,网站cc攻击用什么来做,耐克官网网站设计单一职责原则#xff1a; 就一个类而言#xff0c;应该只有一个引起它变化的原因#xff0c;如果一个类承担的职责过多就等于把这些职责耦合在一起#xff0c;至少会造成以下两方面的问题#xff1a; 我们要去修改该类中的一个职责可能会影响到该类的其它职责。这种耦合…单一职责原则 就一个类而言应该只有一个引起它变化的原因如果一个类承担的职责过多就等于把这些职责耦合在一起至少会造成以下两方面的问题 我们要去修改该类中的一个职责可能会影响到该类的其它职责。这种耦合会导致脆弱的设计当变化发生时设计会遭受到意想不到的破坏。当客户端仅需要该对象的某一个职责时不得不将其他不需要的职责全都包含进来从而造成冗余代码或代码的浪费。 我们在设计一个类时要学会发现职责并把那些职责相互分离其实要去判断是否应该分离出一个类来并不难前面说过一个类应该只有一个引起它变化的原因如果你能想到其它的原因也能去改变这个类那么这个类就具有多于1个的职责就应该考虑类的职责分离。 在之前的这篇博客中https://blog.csdn.net/weixin_44049823/article/details/128907849我们实现的计算器实际上也用到了单一职责原则这里我们选出其中最经典的3.0版本和5.0版本来学习单一职责原则。 3.0版本计算器代码如下 #includeiostream using namespace std; #includestring//业务逻辑 //异常类用于处理异常情况 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版本如下 #includeiostream using namespace std; #includestring//业务逻辑//异常类用于处理异常情况 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.dnsts.com.cn/news/41173.html

相关文章:

  • 保定市住房和城乡建设厅网站容桂品牌网站建设
  • 网站没询盘怎么做推广在手机上怎么注册公司
  • 做网站怎么赚流量智联招聘网站怎么做两份简历模板
  • 网站开发 后端服务网站设计网络推广关键词
  • 如何学好jsp网站开发做画册可以参考哪些网站
  • wordpress 站库分离用织梦模板做网站
  • 网站ico网站建设保定
  • 网站建设方案如何讲解安徽六安房价
  • 罗山网站建设宿迁建设局网站拆除备案
  • 专业网站建设的价格wordpress样式多的编辑器
  • 网站的建设维护推广网站百度收录
  • 西安自助建站哔哩哔哩网页版下载
  • 中国小康建设网站怎么做网站推广图片
  • 自己制作的网站上传到服务器后怎么原来的网页没有变wordpress收录p
  • wordpress还原恢复数据库网络优化岗位详细介绍
  • wordpress资讯网站模板做自己的网站的好处
  • 上海做宴会的网站wordpress低版本主题
  • 南阳建站公司深圳建站公司服务
  • 哈尔滨企业网站国家企业信用公示信息系统官网
  • 永年做网站多少钱怎么做售房网站
  • 建设手机银行的网站手机wordpress清除缓存
  • 百度网站解封专门做超市dm网站
  • 做企业内刊有哪些网站推荐外网网站建设
  • seo如何选择网站标题新品发布会手机
  • 自己建的网站如何做海外推广有哪些效果图做的好的网站
  • tk域名免费注册网站浙江网站建设价位
  • 自己搭建网站要钱吗博达高校网站群建设教程
  • 网站后台 模板深圳专业做网页的公司
  • 点击网站首页域名又添加一个开车搜索关键词
  • 地方房地产网站网络舆情分析报告