文章目录
  1. 1. Move Method(搬移函数)
    1. 1.1. Before Refactoring
    2. 1.2. After Refactoring
  2. 2. Move Field(搬移字段)
    1. 2.1. Before Refactoring
    2. 2.2. After Refactoring
  3. 3. Extract Class(提炼类)
    1. 3.1. Before Refactoring
    2. 3.2. After Refactoring
  4. 4. Inline Class(将类内联化)
    1. 4.1. Before Refactoring
    2. 4.2. After Refactoring
  5. 5. Hide Delegate(隐藏“委托关系”)
    1. 5.1. Before Refactoring
    2. 5.2. After Refactoring
  6. 6. Remove Middle Man(移除中间人)
  7. 7. Introduce Foreign Method(引入外加函数)
    1. 7.1. Before Refactoring
    2. 7.2. After Refactoring
  8. 8. Introduce Local Extension(引入本地扩展)

在对象的设计过程中,“决定把职责放在哪里”即使不是最重要的事,也至少是最重要的事之一。

Move Method(搬移函数)

程序中,有个函数与其所驻类之外的另一个类进行更多交流:调用后者,或者被后者调用。就应该在该函数最常引用的类中建立一个有着类似行为的新函数,将旧函数变成一个单纯的委托函数,或是将旧函数完全移除。

Before Refactoring

class Account ....
    double overdraftCharge() {
        if (_type.isPremium()) {
            double result = 10;
            if (_daysOverdrwan > 7) {
                result += (_daysOverdrwan -7) * 0.85;
            }
            return result;
        } else {
            return _daysOverdrwan * 1.75;
        }
    }

    double bankCharge() {
        double result = 4.5;
        if (_daysOverdrwan > 0) {
            result += overdraftCharge();
        }
        return result;
    }
    private AccountType _type;
    private int _daysOverdrwan;

After Refactoring

class Account ....
    double overdraftCharge() {
        return _type.overdraftCharge(_daysOverdrwan);
    }

    double bankCharge() {
        double result = 4.5;
        if (_daysOverdrwan > 0) {
            result += overdraftCharge();
        }
        return result;
    }
    private AccountType _type;
    private int _daysOverdrwan;

class AccountType...
    double overdraftCharge(int _daysOverdrwan) {
        if (isPremium()) {
            double result = 10;
            if (_daysOverdrwan > 7) {
                result += (_daysOverdrwan -7) * 0.85;
            }
            return result;
        } else {
            return _daysOverdrwan * 1.75;
        }
    }

Move Field(搬移字段)

程序中某个字段被其所驻类之外的另一个类更多地用到。应该在目标类新建一个字段,修改源字段的所有用户,令它们改用新字段。

Before Refactoring

class Account...
    private AccountType _type;
    private double _interestRate;

    double interestForAmount_days(double amount, int days) {
        return _interestRate * amount * days/365;
    }

After Refactoring

class Account...
    private AccountType _type;

    double interestForAmount_days(double amount, int days) {
        return _type.getInterestRate * amount * days/365;
    }

class AccountType...
    private double _interestRate;
    void setInterestRate(double arg) {
        _interestRate = arg;
    }

    double getInterestRate() {
        return _interestRate;
    }

Extract Class(提炼类)

某个类做了应该由两个类做的事。应该建立一个新类,将相关的字段和函数从旧类搬移到新类。

Before Refactoring

After Refactoring

Inline Class(将类内联化)

某个类没有做太多事情。应该将这个类的所有特性搬移到另一个类中,然后移除原类。

Before Refactoring

After Refactoring

和Extract Class相反。

Hide Delegate(隐藏“委托关系”)

客户通过一个委托类来调用另一个对象。应该在服务类上建立客户端所需的所有函数,用以隐藏委托关系。

Before Refactoring

class Person {
    Department _department;
    public Department getDepartment() {
        return _department;
    }

    public void setDepartment(Department arg) {
        _department = arg;
    }
}

class Department {
    private String _chargeCode;
    private Person _manager;

    public Department (Person manager) {
        _manager = manager;
    }

    public Person getManager() {
        return _manager;
    }
}

manager = John.getDepartment().getManager();

After Refactoring

class Person {
    Department _department;
    public Department getDepartment() {
        return _department;
    }

    public void setDepartment(Department arg) {
        _department = arg;
    }

    public Person getManager() {
        return _department.getManager();
    }
}

class Department {
    private String _chargeCode;
    private Person _manager;

    public Department (Person manager) {
        _manager = manager;
    }

    public Person getManager() {
        return _manager;
    }
}

manager = John.getManager();

Remove Middle Man(移除中间人)

某个类做了过多点简单委托动作。应该让客户直接调用受托类。

和Hide Delegate相对。很难说什么程度的隐藏才是合适的。随着系统的变化,“合适的隐藏程度”这个尺度也相应改变。需要不断的重构。

Introduce Foreign Method(引入外加函数)

需要提供服务的类增加一个函数,但是你无法修改这个类。应该在客户端中建立一个函数,并以第一参数形式传入一个服务类实例。

Before Refactoring

Date newStart = new Date(previousEnd.getYear(),previousEnd.getMonth().
    previousEnd.getDate() + 1);

After Refactoring

Date newStart = nextDay(previousEnd);
private static Date nextDay(Date arg) {
    return new Date(arg.getYear, arg.getMonth, arg.getDate + 1);
}

Introduce Local Extension(引入本地扩展)

需要提供服务的类增加一个函数,但是你无法修改这个类。

应该新建一个新类,使它包含这些额外函数。让这个扩展品成为源类的子类或者包装类。

class MfDateSub extends Date {
    public MfDateSub nextDay() ...
    public int dayOfYear() ...
}

或者

class MfDateWrap {
    private Date _original;
}
文章目录
  1. 1. Move Method(搬移函数)
    1. 1.1. Before Refactoring
    2. 1.2. After Refactoring
  2. 2. Move Field(搬移字段)
    1. 2.1. Before Refactoring
    2. 2.2. After Refactoring
  3. 3. Extract Class(提炼类)
    1. 3.1. Before Refactoring
    2. 3.2. After Refactoring
  4. 4. Inline Class(将类内联化)
    1. 4.1. Before Refactoring
    2. 4.2. After Refactoring
  5. 5. Hide Delegate(隐藏“委托关系”)
    1. 5.1. Before Refactoring
    2. 5.2. After Refactoring
  6. 6. Remove Middle Man(移除中间人)
  7. 7. Introduce Foreign Method(引入外加函数)
    1. 7.1. Before Refactoring
    2. 7.2. After Refactoring
  8. 8. Introduce Local Extension(引入本地扩展)