你不知道你不知道的事

我虽新手,就在学习

闲聊31天重构-第1,2天:方法迁移

封装集合 Encapsulate Collection

Java不适用 略

方法迁移(Move Method)

D:原来代码

public class BankAccount{
    public BankAccount(int accountAge, int creditScore, AccountInterest accountInterest){
        AccountAge = accountAge;
        CreditScore = creditScore;
        AccountInterest = accountInterest;
    }
    private int AccountAge;
    private int CreditScore;
    public AccountInterest AccountInterest;

    public double CalculateInterestRate()
    {
        if (CreditScore > 800)
            return 0.02;

        if (AccountAge > 10)
            return 0.03;

        return 0.05;
    }


    public int getAccountAge() {
        return AccountAge;
    }

    public void setAccountAge(int accountAge) {
        AccountAge = accountAge;
    }

    public int getCreditScore() {
        return CreditScore;
    }

    public void setCreditScore(int creditScore) {
        CreditScore = creditScore;
    }

    public AccountInterest getAccountInterest() {
        return AccountInterest;
    }

    public void setAccountInterest(AccountInterest accountInterest) {
        AccountInterest = accountInterest;
    }
}

class AccountInterest{

    private BankAccount Account;

    public double InterestRate(){
        return Account.CalculateInterestRate(); 
    }

    public boolean IntroductoryRate(){
        return Account.CalculateInterestRate() < 0.05; 
    }

    public BankAccount getAccount() {
        return Account;
    }

    public void setAccount(BankAccount account) {
        Account = account;
    }

    public AccountInterest(BankAccount account)
    {
        Account = account;
    }

}

M:这段代码有什么问题?

Z:CalculateInterestRate()方法在AccountInterest类中使用,但是没在BankAccount中用到,所以应该将这个方法迁移到AccountInterest类中

public class BankAccount{
    public BankAccount(int accountAge, int creditScore, AccountInterest accountInterest){
        AccountAge = accountAge;
        CreditScore = creditScore;
        AccountInterest = accountInterest;
    }
    public int AccountAge;
    public int CreditScore;
    public AccountInterest AccountInterest;


    public int getAccountAge() {
        return AccountAge;
    }

    public void setAccountAge(int accountAge) {
        AccountAge = accountAge;
    }

    public int getCreditScore() {
        return CreditScore;
    }

    public void setCreditScore(int creditScore) {
        CreditScore = creditScore;
    }

    public AccountInterest getAccountInterest() {
        return AccountInterest;
    }

    public void setAccountInterest(AccountInterest accountInterest) {
        AccountInterest = accountInterest;
    }
}

class AccountInterest{

    private BankAccount Account;

    public double InterestRate(){
        return CalculateInterestRate(); 
    }

    public boolean IntroductoryRate(){
        return CalculateInterestRate() < 0.05; 
    }

    public double CalculateInterestRate()
    {
        if (Account.CreditScore > 800)
            return 0.02;

        if (Account.AccountAge > 10)
            return 0.03;

        return 0.05;
    }


    public BankAccount getAccount() {
        return Account;
    }

    public void setAccount(BankAccount account) {
        Account = account;
    }

    public AccountInterest(BankAccount account)
    {
        Account = account;
    }

}

M:所以总结来说,一个方法如果用到A类的属性,被B类调用。应该放在B类中。

阅读更多
个人分类: 闲聊Java重构
想对作者说点什么? 我来说一句

31重构学习系列-中文

2011年10月15日 262KB 下载

NET 31重构指南

2009年09月07日 55KB 下载

31重构.pdf

2016年04月13日 392KB 下载

重构 31天学习速成

2009年12月09日 369KB 下载

31天学会CRM项目开发

2017年11月12日 86.14MB 下载

31重构速成

2012年12月03日 394KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭