重构·改善既有代码的设计.03之重构手法(上)

您所在的位置:网站首页 query方法是哪个对象的方法 重构·改善既有代码的设计.03之重构手法(上)

重构·改善既有代码的设计.03之重构手法(上)

2023-03-17 01:26| 来源: 网络整理| 查看: 265

1. 前言

之前的重构系列中,介绍了书中提到的重构基础,以及识别代码的坏味道。今天继续第三更,讲述那些重构手法(上)。看看哪些手法对你的项目能有所帮助… 在这里插入图片描述

2. 重新组织函数

对函数进行整理,使之更恰当的包装代码。

1、Extract Method 提炼函数。

改造前:

void printInfoAndDetail() { this.printInfo(); System.out.println("this is detail name:" + _name); System.out.println("this is detail account:" + _account); }

改造后:

void printInfoAndDetail() { this.printInfo(); this.printDetail(); } void printDetail() { System.out.println("this is detail name:" + _name); System.out.println("this is detail account:" + _account); }

动机: 控制函数的粒度,函数粒度很小,那么被复用的机会就更大;其次会使高层函数读起来就像一系列注释,再次,如果函数都是细粒度,那么函数的覆盖也会更容易。 一个函数多长才算合适?其实长度不是关键问题,关键在于函数名和函数本体之间的语义距离。 做法: 1、创造一个新函数,根据这个函数意图来命名(以它”做什么“来命名,而不是”怎样做“命名)。 只要新的函数名能够以更好的方式昭示代码意图,你也应该提炼他(就算代码只是一条消息,或一个函数调用)。但如果你想不出一个更有意义的名称,就别动。 2、将提炼出来额代码从源函数复制到新建的目标函数中。 3、检查变量。检查提炼出的代码是否引用了源代函数的局部变量或参数。以被提炼函数中是否含有临时变量。 难点: 这个重构手法的难点就在于局部变量的控制,包括传进源函数的参数和源函数所有声明的临时变量。

2、Inline Method 内联函数。

改造前:

int getRating() { return isGe5() ? 2 : 1; } boolean isGe5(){ return _num >= 5; }

改造后:

int getRating() { return _num >= 5 ? 2 : 1; }

动机: 移除非必要的间接层。当然间接层有其价值,但不是所有的间接层都有价值,可以去除那些无用的间接层。 做法: 1、检查函数,确定他不具备多态性。如果有子类继承了这个函数,那就不能将此函数内联。因为子类无法覆盖一个根本不存在的函数。如例子中,子类可以重写isGe5(),但内敛之后的_num > 5 ? 2 : 1是无法重写的,除非你重写了getRating()。 2、找出函数的所有被调用点,将这个函数的所有被调用点都替换为函数本体。

3、Inline Temp 内联临时变量。

改造前:

double price = order.price(); return price > 1000;

改造后:

return order.price() > 1000 4、Replace Temp With Query 以查询取代临时变量。

改造前:

double price = _qu * _item; if(price > 1000){ return price * 0.95; } else { return price * 0.98; }

改造后:

if(getPrice() > 1000){ return getPrice() * 0.95; } else { return getPrice() * 0.98; } double getPrice(){ return _qu * _item; } 5、Introduce Explaining Variable 引入解释性变量。

改造前:

if((platform.indexOf("mac") > -1) && (platform.indexOf("ie") > -1) && resize > 0 ){ // todo... }

改造后:

final boolean isMac = (platform.indexOf("mac") > -1; final boolean isIe = (platform.indexOf("ie") > -1; final boolean resized = resize > 0; if( && isIe && resized){ // todo... } 6、Split Temporary Variable 分解临时变量。

改造前:

double temp = 2 * (_h + _w); System.out.println(temp); temp = _h * _w; System.out.println(temp);

改造后:

final double temp = 2 * (_h + _w); System.out.println(temp); final double area = _h * _w; System.out.println(area); 7、Remove Assignments to Parameters 移除对参数的赋值。

改造前:

int discount(int inputVal) { if(inputVal > 50) inputVal -= 2; }

改造后:

int discount(int inputVal) { int result = inputVal; if(inputVal > 50) result -= 2; } 3. 在对象之间搬移特性

“决定把责任放在哪儿”。

1、Move Method 搬移函数。

如果一个类有太多的行为,或如果一个类于另一个类有太多合作而形成高度耦合,尝试搬移函数。将旧函数变成一个单纯的委托函数,或是将旧函数完全移除。 改造前:

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

改造后:

class Account { private AccountType _type; private int _dayOverdrawn; double overdraftCharge(){ return _type.overdraftCharge(_dayOverdrawn); } } class AccountType { double overdraftCharge(int daysOverdrawn){ if(isPremium()) { double result = 10; if(dayOverdrawn > 7) result += (dayOverdrawn - 7) * 0.85; return result; } else { return dayOverdrawn * 1.75; } } } 2、Move Field 搬移字段。

如果一个字段,在其所驻类之外的另一个类中有更多函数使用了它,就要考虑搬移这个字段。这里的使用可能是设值,取值函数间接进行的。 改造前:

class Account { private AccountType _type; private int _rate; double overdraftCharge(double amount, int days){ return _rate * amount * days / 365; } }

改造后:

class Account { private AccountType _type; double overdraftCharge(){ return _type.getRate() * amount * days / 365; } } class AccountType { private double _rate; void setRate(double r) { this._rate = r; } void getRate() { return _rate; } } 3、Extract Class 提炼类。

建立一个新类,将相关的字段和函数从旧类搬移到新类。一个类应该是一个清楚的抽象,处理一些明确的责任。 改造前:

class Account { private String personName; private String personPhone; private double money; public String getAccountInfo(){ return personName + ",联系方式:" + personPhone + ",余额:" + money; } }

改造后:

class Account { private Person person = new Person(); private double money; public String getAccountInfo(){ return person.getPersonName() + person.personPhone() + ",余额:" + money; } } class Person { private String personName; private String personPhone; public String getPersonName(){ return "联系人:" + personName; } public String getPersonPhone(){ return "联系方式:" + personPhone; } } 4、Inline Class 将类内联化。

将这个类的所有特性搬移到另一个类中,然后移除原类。与Extract Class相反。

5、Hide Delegate 隐藏“委托关系”。

“封装”即使不是对象的最关键特性,也是最关键特性之一。“封装”意味着每个对象都应该尽可能少了解系统的其他部分。 改造前:

class Person { private Department department; public Department getDepartment(){ return department; } } class Department { private Person manager; public Person getManager(){ return manager; } } // 如果客户希望知道某人的经理是谁,那他的调用关系是: xxx.getDepartment().getManager(); // 暴露了部门和经理的委托关系

改造后:

class Person { private Department department; public Department getDepartment(){ return department; } public Person getManager(){ return department.getManager(); } } class Department { private Person manager; public Person getManager(){ return manager; } } // 如果客户希望知道某人的经理是谁,那他的调用关系是:(隐藏了Department) xxx.getManager(); 6、Remove Middle Man 移除中间人。

某个类做了过多的简单委托动作。

7、Introduce Foreign Method 引入外加函数。

当你需要为提供服务的类增加一个函数,但你无法修改这个类。如果你只使用这个函数一次,那么额外编码工作没什么大不了,升值可能根本不需要原本提供服务的那个类。然而,如果你需要多次使用这个函数,就得不断重复这些代码。重复代码是软件万恶之源。 改造前:

Date newStart = new Date(pre.getYear(), pre.getMethod(), pre.getDate() + 1);

改造后:

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

如真实项目中的案例: BeanUtil.copyProperties(),原始方法该行为需要抛异常,且被建议不再使用该方法进行bean复制。 于是引入外加函数:

class BeanUtilExt { public static void copyProperties(Object target, Object source) { try { BeanUtil.copyProperties() } catch (Exception) { // ignored... } } }

这种方式个人不推荐。

8、Introduce Local Extension 引入本地扩展。

当你需要为提供服务的类提供一些额外函数,但你无法修改这个类。

4. 重新组织数据 1、Self Encapsulate Field 自封装字段。

改造前:

private int _low, _high; boolean includes(int arg){ return arg >= _low && arg return _low; } int getHigh(){ return _high; }

直接访问变量好处:代码容易阅读。 间接访问变量好处:子类可以通过重写(覆盖)一个函数而改变获取数据的途径。

2、Replace Data Value with Object 以对象取代数据值。

开发初期,你往往决定以简单的数据项表示简单的情况。但是,随着开发的进行,你可能会发现,这些简单的数据项不再那么简单了。比如你一开始会用字符串来表示“电话号码”,但是随后你会发现,电话号需要“格式化”,“抽取区号”之类的特殊行为。如果这样的数据项只有一两个,你还可以把相关函数放进数据项所属的对象里,但是Duplicate Code和Feature Envy很快就会表现出来。这时,你就应该将数值变为对象。

3、Change Value toReference 将值对象改为引用对象。

你从一个类衍生出许多批次相等的实例,希望将它们替换为同一个对象。

4、Change Reference to Value 将引用对象改为值对象。

你有一个引用对象,很小且不可变,而且不易管理。

5、Replace Array with Object 以对象取代数组。

你有一个数组,其中的元素各自代表不同的东西。 改造前:

String[] row = new String[3]; row[0] = "liver"; row[1] = "15";

改造后:

Performance row = new Performance(); row.setName("liver"); row.setWins(15); 6、Duplicate Observed Data 复制“被监视数据”。

有一些领域数据置身于GUI组件中,而领域函数需要访问这些数据。 将该数据复制到一个领域对象中。建立一个Observer模式,用以同步领域对象和GUI对象内的重复数据。可以使用事件监听器,诸如JAVAFX中的Property。

7、Change Unidirectional Association to Bidirectional 将单向关联改为双向关联。

两个类都需要使用对方特性,但其间只有一条单向链接。

8、Change Bidirectional Association to Unidirectional 将双向关联改为单向关联。

两个类之间有双向关联,但其中一个类如今不再需要另一个类的特性。

9、Replace Magic Number with Symbolic Constant 以字面常量取代魔法值。

改造前:

double count(double a, double b){ return a * 0.95 * b; }

改造后:

double count(double a, double b){ return a * RATE_CONSTANT * b; }

static final double RATE_CONSTANT = 0.95;

10、Encapsulate Field 封装字段。

即面向对象的首要原则之一:封装,或称为“数据隐藏”。 改造前:

public String _name;

改造后:

private String _name; public String getName() { return _name; } public void setName(String name) { this._name = name; } 11、Encapsulate Collection 封装集合。

让这个函数返回该集合的一个只读副本,并在这个类中提供添加/移除集合元素的函数。 改造前:

class Person { List classes; public List getClasses(){ return classes; } public void setClasses(List cls){ this.classes = cls; } }

改造后:

class Person { List classes; public List getClasses(){ return classes; } // setter方法隐藏,避免用户修改集合内容而一无所知 private void setClasses(List cls){ this.classes = cls; } public void addClass(String cls) { classes.add(cls); } public void removeClass(String cls) { classes.remove(cls); } } 12、Replace Record with Data Class 以数据类取代记录。

主要用来应对传统编程环境中的记录结构。

13、Replace Type Code with Class 以类取代类型码。

类中有一个数值类型码,但它并不影响类的行为。 改造前:

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; @Getter @Setter private int _bloodGroup; public Person(int bloodGroup) { _bloodGroup = bloodGroup; } }

改造后:

class Person { public static final int O = BloodGroup.O.getCode(); public static final int A = BloodGroup.A.getCode(); public static final int B = BloodGroup.B.getCode(); public static final int AB = BloodGroup.AB.getCode(); @Getter private int _bloodGroup; public Person(int bloodGroup) { _bloodGroup = BloodGroup.code(bloodGroup); } public void setBloodGroup(int arg) { _bloodGroup = BloodGroup.code(arg); } } 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 = {A, A, B, AB}; @Getter private final int _code; private BloodGroup(int code) { _code = code; } public static BloodGroup code(int arg) { return _values[arg]; } } 14、Replace Type Code with Subclasses 以子类取代类型码。

你有一个不可变的类型码,他会影响类的行为。

15、Replace Type Code with State/Strategy 以State/Strategy取代类型码。

你有一个类型码,它会影响类的行为,但你无法通过继承手法消除他。

5. 小结

到此仅汇总了一半的重构手法,个人觉得部分重构手法是以牺牲一定的代码阅读性为代价。且书中提到的多数重构手法还是要视具体编程场景而定。避免错误引用。 重构手法和设计模式一样,均为编程模式中的最佳实践。是符合大多数场景和行为的思想或方法的总结。记住是大多数。了解最佳实践有助于提高平常的编码习惯以及提升代码的维护性,可修改性。但如果被错误引用,程序将因为过度设计或引用而变得臃肿。 持续更新中…将继续更新重构手法(下)…



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3