中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 软件工程 > 综合文章
RefactoringtoPatterns:ChainConstructors
作者:Kerievsky 时间:2001-12-30 11:52 出处:互联网 责编:chinaitpower
              摘要:RefactoringtoPatterns:ChainConstructors

Chain Constructors(串联构造子)

撰文/Joshua Kerievsky         编译/透明

 

你拥有多个构造子,其中包含了重复的代码。

将构造子串在一起,以使重复代码减到最少。

public class Loan {

...

public Loan(float notional, float outstanding, int rating, Date expiry) {

this.strategy = new TermROC();

this.notional = notional;

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 = maturity;

         }

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 Loan {

...

public Loan(float notional, float outstanding, int rating, Date expiry) {

this(new TermROC(), notional, outstanding, rating, expiry, null);

}

public Loan(float notional, float outstanding, int rating, Date expiry, Date maturity) {

this(new RevolvingTermROC(), notional, outstanding, rating, expiry, maturity);

}

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;

}

}

动机

在同一个类的两个或更多的构造子中编写重复代码,这就是在为自己埋下麻烦的种子。别人会在你的类中添加新的变量,然后更新一个构造子来对这个变量进行初始化,但是却忘了更新别的构造子。于是,“砰”的一声,向新的bug问好吧。一个类中的构造子越多,代码的重复就会伤害你越重。如果有可能,就应该尽量减少或去除代码重复,这样做的额外好处就是可以帮助你的代码系统减肥

为了达到这个目标,我们经常会使用Constructor Chaining模式进行重构:特化的(specific)构造子调用普化的(general-purposed)构造子,重复这个过程,直到最普化的构造子也被调用到。如果你的每条调用链的末端都是同一个构造子,我就把它叫做“catch-all”构造子,因为它处理了所有构造子的调用。这个catch-all构造子通常会比其他构造子接受更多的参数,并且可能是(也可能不是)私有的或保护的。

如果你发现多个构造子降低了类的可用性,请考虑使用“用Factory Method模式替换多个构造子”的重构方法。

通信

重复

简化

如果一个类中的多个构造子在实现重复的工作,那么在特化与普化的通信上,你的代码就是失败的。要实现这种通信,就应该让特化的构造子调用普化的,并让每个构造子都做自己独一无二的工作。

构造子中的重复代码让你的类更容易出错、更难维护。寻找通用的功能,将它放到普化的构造子中,将调用转发给这些普化的构造子,并在其他的构造子中实现用途不广泛的功能。

如果超过一个的构造子包含同样的代码,要看出构造子之间的区别就很困难了。让特化的构造子调用普化的,并形成一条调用链,从而对构造子进行简化。

过程

1.       寻找包含重复代码的两个构造子(我把它们分别叫做AB)。确定A是否可以调用B或者B是否可以调用A,这样重复代码才可以被安全的(和比较容易的)从这某一个构造子中删掉。

2.       编译、测试。

3.       对类中的每个构造子,重复步骤12,以获得尽量少的重复代码。

4.       如果某个构造子不必要成为公开的,就改变它的可见性。

5.       编译、测试。

范例

1.       我们将从一段简单代码——一个Loan类——开始。这个类有三个构造子,用来表现三种不同类型的贷款业务。大量丑陋的重复代码。

public Loan(float notional, float outstanding, int rating, Date expiry) {

this.strategy = new TermROC();

this.notional = notional;

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 = maturity;

}

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 Loan(float notional, float outstanding, int rating, Date expiry) {

this(new TermROC(), notional, outstanding, rating, expiry, null);

}

2.       编译程序,测试这次修改是否正常工作。

3.       重复步骤12,尽量去除代码重复。这引出了第二个构造子,它也调用第三个构造子,如下所示:

public Loan(float notional, float outstanding, int rating, Date expiry, Date maturity) {

this(new RevolvingTermROC(), notional, outstanding, rating, expiry, maturity);

}

       现在我知道第三个构造子就是我的catch-all构造子,因为它处理了所有的构造细节。

4.       检查这三个构造子所有的调用者,以确定是否可以改变某一个的可见性。在这个案例中,我不能这样做(假设是这样——在这里你无法知道其他代码的情况)。

5.       编译并测试,完成此次重构。

参考书目

[Alex77]         Alexander, C., Ishikawa, S., Silverstein, M., A Pattern Language, New York: Oxford University Press, 1977.

[Fowler99]     Fowler, M., Beck, K., Brant, J., Opdyke, W., Roberts, D., Refactoring: Improving the Design of Existing Code, Reading Mass.: Addison-Wesley, 1999.

[GoF95]         Gamma, E., Heml, R., Johnson, R., Vlissides, J., Design Patterns: Elements of Reusable Object-Oriented Software, Reading Mass.: Addison-Wesley, 1995. 中译本:《设计模式:可复用面向对象软件的基础》,李英军等译,机械工业出版社,20009月。

 

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有