Move Method
如果一个method与另一个class进行了更多的交流(调用或被调用),则需要把它Move到该class中。从而达到高内聚、低耦合的作用。在进行Move Method时需要注意的几点:
1.检查该method使用的一切feature(包括field, method等),如果需要的话,一并move多个method
2.检查该class是否有subclass或superclass,如果使用到了多态,则不能进行这项重构
3.如果method用到了field,则把该field当作method的参数传入新的method
4.如果method用到了其它的method,则把该class当作method的参数传入新的method,在新的method里根据这个引用,调用其它的method
Move Field
某个field被另外一个class更多的用到,则把该field移动到该class中。方法很简单,现对该field建立getter和setter,然后把所有使用该field的地方都替换为getter或setter,再然后就在目标class中建立新的field,最后把原class的getter, setter改为对新getter, setter的简单代理。如果需要,可以把所有使用原getter, setter的地方都替换为新class的getter, setter。
Extract Class
当一个class做了应该由两个class做的事时,应该Extract Class。有几种情况:
1.class太大
2.某些field和method总是一起出现,彼此紧密关联,比如Person类中的电话区号和电话号码,这时应该extract一个TelephoneNumber类
3.如果某些特性需要这样subtyping,而某些特性需要那样subtyping,则可能需要分解原来的class了
方法是先Move Field,再Move Method。
Inline Class
与Extract Class相反,当一个class没有承担足够责任的时候用。
Hide Delegate
将person.getDepartment().getManager() -> person.getManager()。后者是前者的简单代理。对调用者隐藏Department对象。为的是进一步封装,不让调用者了解更多细节。
Remove Middle Man
与Hide Delegate相反。如果某个class做了过多的简单代理工作,完全成了中间人,则需要暴露被代理的对象,让其它class直接调用。有两点:
1.把握一个合适的隐藏程度,在合适的时间使用Hide Delegate或Remove Middle Man
2.两者是不冲突的,可以根据具体情况保留一部分代理
Introduce Foreign Method
使用的某个server class需要一个额外的函数,但是无法修改这个class,此时需要在client class中建立一个额外的函数提供这个功能。个人感觉经常做这个事,不用特别记住。
Introduce Local Extension
对于Introduce Foreign Method的情况,如果需要引入的函数太多,则需要用Introduce Local Extension,通过继承(subclass)或组合(wrapper)的方式扩展server class。两种方式各有优缺点,可根据具体情况选用。一般subclass的方式工作量较小。
分享到:
相关推荐
第 7 章:在对象之间移动特性(Moving Features Between Objects) 第 8 章:重新组织你的数据(Organizing Data) 第 9 章:简化条件表达式(Simplifying Conditional Expressions) 第 10 章:简化函数...
The heart of the book is a catalog of refactorings, organized in chapters on composing methods, moving features between objects, organizing data, simplifying conditional expressions, and dealing with...
Chapter 7:Moving Features Between Objects 在对象之间移动特性 *Move Method 移动方法 Move Field 移动字段 Extract Class 提取类 Inline Class 内联类 Hide Delegate 隐藏委托类 Remove ...
Chapter 7:Moving Features Between Objects 在对象之间移动特性 *Move Method 移动方法 Move Field 移动字段 Extract Class 提取类 Inline Class 内联类 Hide Delegate 隐藏委托类 Remove ...
Chapter 7:Moving Features Between Objects 在对象之间移动特性 *Move Method 移动方法 Move Field 移动字段 Extract Class 提取类 Inline Class 内联类 Hide Delegate 隐藏委托类 Remove ...
Chapter 7:Moving Features Between Objects 在对象之间移动特性 *Move Method 移动方法 Move Field 移动字段 Extract Class 提取类 Inline Class 内联类 Hide Delegate 隐藏委托类 ...
Chapter 7:Moving Features Between Objects 在对象之间移动特性 *Move Method 移动方法 Move Field 移动字段 Extract Class 提取类 Inline Class 内联类 Hide Delegate 隐藏委托类 ...
Moving objects is normally better than copying them. There’s a lot to learn about C++11, not to mention C++14. More importantly, there’s a lot to learn about making effective use of the new ...
Covers all aspects of managing user objects, including creating, renaming, moving, resetting passwords, unlocking, modifying the profile attributes, and locating users that have certain criteria (e.g....
Online Learning and Robust Visual Tracking using Local Features and Global Appearances of Video Objects Chapter 6 Switching Local and Covariance Matching for Efficient Object Tracking Chapter 7 ...
- FIX: With little difference between curve angles at smaller scales the curve was drawn backwards (error in TFlexEllipse.Paint). - ADD: Support of the binary document format. Added the public ...
The Force.com Migration Tool is a powerful utility for moving metadata between local directories and Salesforce environments. It supports files such as: - **Apex Classes**: Code files that define ...
Communication between Contexts 462 Remote Objects, Clients, and Servers 462 Remote Objects 462 A Simple Server 464 A Simple Client 465 .NET Remoting Architecture 466 Channels 466 Formatters 470 ...
The course follows a structured approach, starting with fundamental concepts and gradually moving towards more complex topics. The sequence ensures that learners build a solid foundation before ...
This project involves creating a character that can hop between barrels in a level. - **Creating Our Character**: Designing a character actor and adding necessary components. - **Bringing Our ...
The Compulsory Features List Section 1.4. What's Python Good For? Section 1.5. What's Python Not Good For? Part I: System Interfaces Chapter 2. System Tools Section 2.1. "The os.path to ...