控制圈复杂度种重构技术

上传人:平*** 文档编号:47278583 上传时间:2018-07-01 格式:PPT 页数:13 大小:389.02KB
返回 下载 相关 举报
控制圈复杂度种重构技术_第1页
第1页 / 共13页
控制圈复杂度种重构技术_第2页
第2页 / 共13页
控制圈复杂度种重构技术_第3页
第3页 / 共13页
控制圈复杂度种重构技术_第4页
第4页 / 共13页
控制圈复杂度种重构技术_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《控制圈复杂度种重构技术》由会员分享,可在线阅读,更多相关《控制圈复杂度种重构技术(13页珍藏版)》请在金锄头文库上搜索。

1、www.h uawei.c omHUAWEI Confidential Security Level:内部公开 控制圈复杂度的9种重构技术-Refactoring: Improving the Design of Existing CodeHUAWEI Confidential 文档密级: 内部公开控制圈复杂度:重构可以直接降低圈复杂度的9种重构技术(针对结构化编程): Composing Methods(重新组织你的函数) Extract Method(提炼函数) Substitute Algorithm(替换你的算法) Simplifying Conditional Expressions

2、(简化条件表达式) Decompose Conditional(分解条件式) Consolidate Conditional Expression(合并条件式) Consolidate Duplicate Conditional Fragments(合并重复的条件片断) Remove Control Flag(移除控制标记) Making Method Calls Simpler(简化函数调用) Separate Query from Modifier(将查询函数和修改函数分离) Parameterize Method(令函数携带参数) Replace Parameter with Expli

3、cit Methods(以明确函数取代参数) - Refactoring: Improving the Design of Existing Code 针对面向对象编程: Replace Conditional with Polymorphism(以多态取代条件式)HUAWEI Confidential 文档密级: 内部公开Extract Method(提炼函数)void printOwing(double previousAmount)Enumeration e = _orders.elements();double outstanding = previousAmount * 1.2;/

4、print bannerSystem.out.println (“*“);System.out.println (“* Customer Owes *“);System.out.println (“*“);/ calculate outstandingwhile (e.hasMoreElements() Order each = (Order) e.nextElement();outstanding += each.getAmount();/print detailsSystem.out.println (“name:“ + _name);System.out.println (“amount

5、“ + outstanding); void printOwing(double previousAmount) printBanner();double outstanding = getOutstanding(previousAmount * 1.2);printDetails(outstanding); void printBanner() / print bannerSystem.out.println (“*“);System.out.println (“* Customer Owes *“); double getOutstanding(double initialValue) d

6、ouble result = initialValue;Enumeration e = _orders.elements();while (e.hasMoreElements() Order each = (Order) e.nextElement();result += each.getAmount();return result; void printDetails (double outstanding)System.out.println (“name:“ + _name);System.out.println (“amount“ + outstanding); 将这段代码放进一个独立

7、函数中,并让函数名称解释该函数的用途你有一段代码可以被组织在一起并独立出来HUAWEI Confidential 文档密级: 内部公开Substitute Algorithm(替换你的算法)String foundPerson(String people) for (int i = 0; i 12) return 0;if (_isPartTime) return 0;/ compute the disability amountdouble disabilityAmount() if (isNotEligableForDisability() return 0;/ compute the d

8、isability amount将这些判断合并为一个条件式,并将这个条件式提炼成为一个独立函数你有一系列条件判断,都得到相同结果HUAWEI Confidential 文档密级: 内部公开Consolidate Duplicate Conditional Fragments(合并重复的条件片断)if (isSpecialDeal() total = price * 0.95;send(); else total = price * 0.98;send();if (isSpecialDeal()total = price * 0.95; elsetotal = price * 0.98; sen

9、d();在条件式的每个分支上有着相同的一段代码。将这段重复代码搬移到条件式之外HUAWEI Confidential 文档密级: 内部公开Remove Control Flag(移除控制标记)void checkSecurity(String people) boolean found = false;for (int i = 0; i 100) result += (Math.min (lastUsage(),200) - 100) * 0.05;if (lastUsage() 200) result += (lastUsage() - 200) * 0.07;return new Doll

10、ars (result); Dollars baseCharge() double result = usageInRange(0, 100) * 0.03;result += usageInRange (100,200) * 0.05;result += usageInRange (200, Integer.MAX_VALUE) * 0.07;return new Dollars (result); int usageInRange(int start, int end) if (lastUsage() start) return Math.min(lastUsage(),end) -sta

11、rt;else return 0; 若干函数做了类似的工作,但在函数本体中却包含了不同的值建立单一函数,以参数表达那些不同的值HUAWEI Confidential 文档密级: 内部公开Replace Parameter with Explicit Methods(以明确函数取代参数)void setValue (String name, int value) if (name.equals(“height“)_height = value;if (name.equals(“width“)_width = value;Assert.shouldNeverReachHere(); void se

12、tHeight(int arg) _height = arg; void setWidth (int arg) _width = arg; 函数实现完全取决于参数值而采取不同反应针对该参数的每一个可能值,建立一个独立函数HUAWEI Confidential 文档密级: 内部公开Replace Conditional with Polymorphism(以多态取代条件式)double getSpeed() switch (_type) case EUROPEAN:return getBaseSpeed();case AFRICAN:return getBaseSpeed() - getLoadFactor() *_numberOfCoconuts;case NORWEGIAN_BLUE:return (_isNailed) ? 0 : getBaseSpeed(_voltage);throw new RuntimeException (“Should be unreachable“); 你手上有个条件式,它根据对象类型的不同而选择不同的行为将整个条件式的每个分支放进一个子类的重载方法中,然后将原始函数声明为抽象方法HUAWEI Confidential 文档密级: 内部公开Thank you!

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 教学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号