免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2740 | 回复: 0

[zt]Chain Constructors(串联构造子) [复制链接]

论坛徽章:
0
发表于 2002-09-30 09:38 |显示全部楼层
Chain Constructors(串联构造子)
撰文/Joshua Kerievsky         编译/透明



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

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

public class Loan {

...

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

this.strategy = new TermROC()&#59;

this.notional = notional&#59;

this.outstanding = outstanding&#59;

this.rating =rating&#59;

this.expiry = expiry&#59;

}

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

this.strategy = new RevolvingTermROC()&#59;

this.notional = notional&#59;

this.outstanding = outstanding&#59;

this.rating = rating&#59;

this.expiry = expiry&#59;

this.maturity = maturity&#59;

         }

public Loan(CapitalStrategy strategy, float notional, float outstanding,

int rating, Date expiry, Date maturity) {

this.strategy = strategy&#59;

this.notional = notional&#59;

this.outstanding = outstanding&#59;

this.rating = rating&#59;

this.expiry = expiry&#59;

this.maturity = maturity&#59;

}

}






public class Loan {

...

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

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

}

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

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

}

public Loan(CapitalStrategy strategy, float notional, float outstanding,

int rating, Date expiry, Date maturity) {

this.strategy = strategy&#59;

this.notional = notional&#59;

this.outstanding = outstanding&#59;

this.rating = rating&#59;

this.expiry = expiry&#59;

this.maturity = maturity&#59;

}

}


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

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

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

通信
重复
简化

如果一个类中的多个构造子在实现重复的工作,那么在特化与普化的通信上,你的代码就是失败的。要实现这种通信,就应该让特化的构造子调用普化的,并让每个构造子都做自己独一无二的工作。
构造子中的重复代码让你的类更容易出错、更难维护。寻找通用的功能,将它放到普化的构造子中,将调用转发给这些普化的构造子,并在其他的构造子中实现用途不广泛的功能。
如果超过一个的构造子包含同样的代码,要看出构造子之间的区别就很困难了。让特化的构造子调用普化的,并形成一条调用链,从而对构造子进行简化。


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

2.       编译、测试。

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

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

5.       编译、测试。

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

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

this.strategy = new TermROC()&#59;

this.notional = notional&#59;

this.outstanding = outstanding&#59;

this.rating = rating&#59;

this.expiry = expiry&#59;

}

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

this.strategy = new RevolvingTermROC()&#59;

this.notional = notional&#59;

this.outstanding = outstanding&#59;

this.rating = rating&#59;

this.expiry = expiry&#59;

this.maturity = maturity&#59;

}

public Loan(CapitalStrategy strategy, float notional, float outstanding, int rating,

Date expiry, Date maturity) {

this.strategy = strategy&#59;

this.notional = notional&#59;

this.outstanding = outstanding&#59;

this.rating = rating&#59;

this.expiry = expiry&#59;

this.maturity = maturity&#59;

}

我研究了前面的两个构造子。它们包含重复的代码,而第三个构造子也同样。我考虑第一个构造子应该调用哪一个,并发现它应该调用第三个构造子。因此我把第一个构造子修改成:

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

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

}

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

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

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

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

}

       现在我知道第三个构造子就是我的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. 中译本:《设计模式:可复用面向对象软件的基础》,李英军等译,机械工业出版社,2000年9月。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP