`
irican
  • 浏览: 31644 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Moving Features Between Objects

阅读更多
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 章:简化函数...

    Refactoring

    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 隐藏委托类   ...

    重构-改善既有代码的设计+中文版

    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 隐藏委托类   ...

    Active Directory Cookbook, 3rd Edition.pdf

    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....

    Object Tracking

    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 ...

    A Practical Introduction to Computer Vision with OpenCV(Wiley,2014)

    video processing to detect and model moving objects; geometric operations & camera models; edge detection; features detection; recognition in images Contains a large number of vision application ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - 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 ...

    Professional C# 3rd Edition

    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 ...

    Python程序设计(第二版).chm

    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 ...

    Debugging with GDB --2002年5.11

    r Switching between source languages s List of filename extensions and languages s Setting the working language s Having GDB infer the source language r Displaying the language r Type and range ...

    Beginning Python (2005).pdf

    Using Python Objects from C Code 380 Summary 383 Exercises 383 Chapter 18: Writing Shareware and Commercial Programs 385 A Case Study: Background 385 How Much Python Should You Use? 386 02_...

    UG6.0快捷键大全

    POPUP_MESSAGE Groups the selected features into a collection called a feature set. ACTIONS STANDARD END_OF_AFTER MENU UG_ARRANGE BUTTON/HIDE UG_MODELING_EDIT_GROUP_FEATURE LABEL Edit... ...

    C++ 标准 ISO 14882-2011

    Contents Contents iii List of Tables xi List of Figures xv 1 General 1 1.1 Scope . . . . ....1.2 Normative references ....1.3 Terms and definitions ....1.4 Implementation compliance ....1.5 Structure of this ...

Global site tag (gtag.js) - Google Analytics