重构

一.什么是重构?

A series of small steps, each of which changes the program’s internal structure without changing its external behavior。

重构是一系列的小部骤,每一步只修改程序的内部结构,但是不改变它的外部行为.

二.为什么要重构

  • To improve the software design
  • 为了提高软件设计

-Combat's  "it rot"

抵抗代码的腐化

        -Makes the program easier to change

使程序更容易修改

  • To make the software easier to understand
  • 使软件更容易理解

-Write for people, not the compiler

程序是写给人看的,不是给编译器看的.

-Understand unfamiliar code

理解不熟悉的代码

  • To help find bugs
  • 帮助找到bug

-Refactor while debugging to clarify the code

在为了理清代码而DEBUG时,进行重构.

三.我们应该在什么时候重构?

  • To add new functionality
  • 添加新的功能时

-Refactor existing code until you understand it

重构现有的代码直到你理解它们

-Refactor the design to make it easy to add

重构设计使它容易添加新的功能

  • To find bugs
  • 在发现bug时

-Refactor to understand the code

重构直到你理解代码

  • For code reviews
  • 为了代码评审

-Rmmediate effect of code review

-Allows for higher level suggestions

Don’t set aside time for refactoring,include it in your normal activities

在日常活动中进行重构,而不是另外找时间去重构.

四.最后的思想

  • The one benefit of objects is that they make it easier to change.

  • 有一个好处就是使得对象更容易修改.

  • Refactoring allows you to improve the design after the code is written

  • 重构允许你在代码已经写完后改进自己的设计.

  • Up front design is still important,but not so critical

  • 事先的设计仍然是很重要的,但是并不那么关键了.

五.例子中用到的的重构条目:

  • A.抽取临时变量:

  • B.使用查询方法代替临时变量:

Find temp with a single assignment

Extract Right Hand Side of assignment

Replace all references of temp with new method

Remove declaration and assignment of temp

Compile and test

  • C.抽取方法:

Create method named after intention of code

Copy extracted code

Look for local variables and parameters

Turn into parameter

Turn into return value

Declare within method

Compile

Replace code fragment with call to new method

Compile and test

  • D.移动方法:

Declare method in target class

Copy and fit code

Set up a reference from the source object to the target

Turn the original method into a delegating method

-amountOf(Rental each) {return each.charge()}

-Check for overriding methods

Compile and test

Find all users of the method

    -Adjust them to call method on target

Remove original method

Compile and test

  • E.使用状态模式/策略模式代替type code,也就是去掉switch:

Create a new state class for the type code

Add subclasses of the state object, one for each type code

Create an abstract query in the superclass to return the type code. Override in subclasses to return correct type code

Compile

Create field in old class for the state object

Change the type code query to delegate to the state object

Change the type code setting methods to assign an instance of the subclass

Compile and test

  • F.使用多态代替switch语句

Move switch to superclass of inheritance structure

Copy one leg of case statement into subclass

Compile and test

Repeat for all other legs

Replace case statement with abstract method

  • G.形成模版方法

Take two methods with similar overall structure but varying pieces

Use subclasses of current class, or create a strategy and move the methods to the strategy

At each point of variation extract methods from each source with the the same signature but different body

Declare signature of extracted method in superclass and place varying bodies in subclasses

When all points of variation have been removed,move one source method to superclass and remove the other

     六.例子中提到的模式:

策略模式

模版方法模式

状态模式

posted on 2008-12-17 11:53 JavaBegger 阅读(74) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

导航

统计

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜