重构一书问题笔记

来源:互联网 发布:2016网络超级红人节 编辑:程序博客网 时间:2024/04/28 17:05

几个不需要看的地方

以下几个比较特殊,感觉用处并没有那么广。

Change value to Reference 将值对象改为引用对象??

Change Reference to Value 将引用对象改为值对象???

Duplicate Observed data 复制被监视数据???

Change Unidirection Association to Bidirectional 将单向关联改为双向关联??

Change Bidirectional Association to Unidirection将双向关联改为单向关联??

Introduce Foreign Method 引入外加函数???

类型码重构

1.Replace Type Code with Class 以类来取代类型码

public class Person {    public static final int O = 0;    public static final int A = 1;    public static final int B = 2;    public static final int AB = 3;    private int bloodGroup;    public Person(int bloodGroup) {        this.bloodGroup = bloodGroup;    }    public int getBloodGroup() {        return bloodGroup;    }    public void setBloodGroup(int bloodGroup) {        this.bloodGroup = bloodGroup;    }

重构后的情景

public class Person {    private BloodGroup bloodGroup;    public BloodGroup getBloodGroup() {        return bloodGroup;    }    public Person(BloodGroup bloodGroup) {        this.bloodGroup = bloodGroup;    }    public void setBloodGroup(BloodGroup bloodGroup) {        this.bloodGroup = bloodGroup;    }}
public class BloodGroup {    public static final BloodGroup O = new BloodGroup(0);    public static final BloodGroup A = new BloodGroup(1);    public static final BloodGroup B = new BloodGroup(2);    public static final BloodGroup AB = new BloodGroup(3);    private static final BloodGroup[] values = {O, A, B, AB};    private final int code;    private BloodGroup(int code) {        this.code = code;    }    private static BloodGroup code(int arg) {        return values[arg];    }    public int getCode() {        return code;    }}

这样:
之前的Person person = new Person(Person.A);
就变成了Person person = new Person(BloodGroup.A);
之前的person.getBloodGroupCode()变成了person.getBloodGroup().getCode()

2.Replace Type Code with Subclasses 以子类来取代类型码???

public class Employee {    private int type;    static final int ENGINEER = 0;    static final int SALESMAN = 1;    static final int MANAGER = 2;    private Employee (int type) {        this.type = type;    }//    public int getType() {//        return type;//    }////    static Employee create(int type) {//        return new Employee(type);//    }}

重构后:

public abstract class Employee {    static final int ENGINEER = 0;    static final int SALESMAN = 1;    static final int MANAGER = 2;    Employee() {    }    abstract int getType();    static Employee create(int type) {        switch (type) {            case ENGINEER:                return new Engineer();            case SALESMAN:                return new Salesman();            case MANAGER:                return new Manager();            default:                throw new IllegalStateException("incorrect type code values");        }    }}
public class Engineer extends Employee {    public Engineer() {        super();    }    @Override    int getType() {        return super.ENGINEER;    }}

3.Replace Type Code with State/Strategy 以状态/策略取代类型码????

参考第一章,就理解了

Replace Subclass with Fieldls 以字段取代子类???

Replace Method with Method object 函数对象取代函数???

理解

Replace Parameter with Explicit Methods 以明确函数取代参数???(最好使用后面的例子,而不是图)

明白

0 0
原创粉丝点击