重构与模式_ppt [兼容模式].pdf
《重构与模式_ppt [兼容模式].pdf》由会员分享,可在线阅读,更多相关《重构与模式_ppt [兼容模式].pdf(25页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、重构与模式重构与模式何谓重构何谓重构?重构是一种对软件内部结构的改善,目的是在不改变软件的可见行为的情况下,使其更理解,修改成本更低。可读性好的代码可读性好的代码?Ward Cuinningham?创造了CRC(Class-Responsibility-Collaboration)卡?Wiki Web?极限编程?根据日期进行检查?看看可读性好的代码?november(20,2005)?调用了 public void Date november(int day,int year)?还可以直接这样实现?java.util.Calendar c=java.util.Calendar.getInsta
2、nce();?c.set(2005,java.util.Calendar.NOVEMBER,20);?c.getTime();?但可读性好的代码?读起来像自然语言?将重要代码与分散注意力的代码分离开来不好的代码不好的代码?最常见的设计问题都出自这样的代码?重复?不清晰?复杂面对很多包含重复代码的构造函数面对很多包含重复代码的构造函数public class Loan public Loan(float notional,float outstanding,int rating,Date expiry)this.strategy=new TermROC();this.notional=notio
3、nal;this.outstanding=outstanding;this.rating=rating;this.expiry=expiry;public Loan(float notional,float outstanding,int rating,Date expiry,Date maturity)this.strategy=new RevolvingTermROC();this.notional=notional;this.outstanding=outstanding;this.rating=rating;this.expiry=expiry;this.maturity=maturi
4、ty;public Loan(CapitalStrategy strategy,float notional,float outstanding,int rating,Date expiry,Date maturity)this.strategy=strategy;this.notional=notional;this.outstanding=outstanding;this.rating=rating;this.expiry=expiry;this.maturity=maturity;把构造函数链接起来,从而获得最少的代码重复把构造函数链接起来,从而获得最少的代码重复public class
5、 Loan public Loan(float notional,float outstanding,int rating,Date expiry)this(new TermROC(),notional,outstanding,rating,expiry,maturity);public Loan(float notional,float outstanding,int rating,Date expiry,Date maturity)this(new RevolvingTermROC(),notional,outstanding,rating,expiry,maturity);public
6、Loan(CapitalStrategy strategy,float notional,float outstanding,int rating,Date expiry,Date maturity)this.strategy=strategy;this.notional=notional;this.outstanding=outstanding;this.rating=rating;this.expiry=expiry;this.maturity=maturity;用用Factory Method引入多态创建引入多态创建?一个层次中的类都相似地实现一个方法,只是对象创建的步骤不同DOMBui
7、lderTest+testAddAboveRoot():voidXMLBuilderTest+testAddAboveRoot():voidbuilder=new DOMBuilder(“order”);builder=new XMLBuilder(“order”);用用Factory Method引入多态创建引入多态创建?创建调用Factory Method来处理实例化的方法的唯一超类版本AbstractBuilderTest#builder:OUtputBuilderbuilder=createBuilder(“orders”)#createBuilder(rootName:String)
8、:OutputBuilder+testAddAboveRoot():voidDOMBuilderTest#createBuilder(rootName:String):OutputBuilderXMLBuilderTest#createBuilder(rootName:String):OutputBuilder用用Strategy替换条件逻辑替换条件逻辑Loancapital:doublecapital()if(expiry=null&maturyty!=null)return commitment*duration()*riskFactor();用用Strategy替换条件逻辑替换条件逻辑L
9、oancapital:doublecapital()return capitalStrategy.capital(this);为每个变体创建一个Strategy并使方法把计算委托到Strategy实例CapitalStrategycapital(loan:Loan):doubleCapitalStrategyAdvisedLine capital(loan:Loan):doubleCapitalStrategyRevolvercapital(loan:Loan):double形成形成Template Method?子类中的两个方法以相同的顺序执行相似的步骤,但是步骤并不完全相同CapitalS
10、trategycapital(loan:Loan):doubleCapitalStrategyAdvisedLine capital(loan:Loan):doubleCapitalStrategyTermLoancapital(loan:Loan):doubleReturn loan.getCommitment()loan.getUnusedPercentage()duration(loan)riskFactorfor(loan);Return loan.getCommitment()duration(loan)riskFactorfor(loan);形成形成Template Method?
11、通过把这些步骤提取成具有相同签名的方法来泛化这两个方法,然后上移这些泛化方法,形成Template MethodCapitalStrategycapital(loan:Loan):doubleduration(loan:Loan):doubleriskFactorfor(loan):doubleriskAmountFor(loan:Loan):doubleCapitalStrategyAdvisedLine riskAmountFor(loan:Loan):doubleCapitalStrategyTermLoanduration(loan:Loan):double riskAmountFor
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 兼容模式 重构与模式_ppt 兼容模式 模式 _ppt 兼容
限制150内