网站公告列表

  没有公告

加入收藏
设为首页
在线投稿

您现在的位置: IT知识网 >> IT知识 >> 维修维护 >> 其它外设维护维修 >> 文章正文

 

  zt Chain Constructors(串联构造子)           

zt Chain Constructors(串联构造子)
作者:佚名 文章来源:不详 点击数: 更新时间:2006-12-6 2:34:36
    zt Chain Constructors(串联构造子)
    zt Chain Constructors(串联构造子)    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;
tis.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.       寻找包含重复代码的两个构造子(我把它们分别叫做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();
this.notional = notional;
ths.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.       重复步骤1和2,尽量去除代码重复。这引出了第二个构造子,它也调用第三个构造子,如下所示:
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. 中译本:《设计模式:可复用面向对象软件的基础》,李英军等译,机械工业出版社,2000年9月。
  

文章录入:wuyongjian    责任编辑:wuyongjian 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最 新 热 门
    相 关 文 章
    CISCO7200路由器MultiCh
    SWITCH 命令大全
    ChinaNet路由政策
    基于Softswitch的组网及
    常见TCP/UDP端口收录大全
    常见TCP/UDP端口收录大全
    无法连接到ADSLSwitch的
    TOE技术以及TCP网卡的工
    CCNA手册:交换机(Switch
    Routing Switch VS. Swi
     
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    Copyright© ITZS.NET All Rights Reserved
    QQ:272895858   ICP备案编号:吉ICP备07000044号
    IT知识网 站长:博浪