public class BankAccount {
public void CreateAccount(Customer customer, boolean withChecking, boolean withSavings, boolean withStocks) {
// do work
}
}
要想使这样的代码运行得更好,我们可以通过命名良好的方法暴露布尔参数,并将原始方法更改为private
以阻止外部调用。显然,你可能需要进行大量的代码转移,也许重构为一个Parameter Object会更有意义。
public class BankAccount {
public void CreateAccountWithChecking(Customer customer) {
CreateAccount(customer, true, false);
}
public void CreateAccountWithCheckingAndSavings(Customer customer) {
CreateAccount(customer, true, true);
}
private void CreateAccount(Customer customer, Boolean withChecking, Boolean withSavings) {
// do work
}
}