Wangtao's Blog

# Chain Constructors

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;

}

}

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;

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.       重复步骤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.       编译并测试，完成此次重构。

#### Autofuc None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructor

2017-03-17 16:02:55

#### constructors not allowed a return type错误问题

2016-07-30 09:40:25

#### error C2533: ' ::{ctor}' : constructors not allowed a return type

2011-02-13 15:07:00

#### spring使用cglib代理需要注意的问题

2010-03-03 23:38:00

#### Avoid non-default constructors in fragments

2014-11-13 12:38:50

#### None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'

2015-09-01 10:15:17

#### C++：类的语法错误 error c2533：constructors not allowed a return type（构造函数不允许返回一个类型）

2016-04-10 09:44:53

#### Spring AOP - Caused by: java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

2009-10-13 08:07:00

#### 【工具教程】MyEclipse教程

2012-06-11 20:44:00

#### Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题

2018-03-18 17:22:36