Refactoring Read Note (3)

Target

Refactoring book 閱讀筆記

Action Items

  • CH 11 Deal with Generalization

Quick Start

CH 11 Deal with Generalization

11.1 Pull Up Field

兩個 subclasses 擁有相同的欄位。
將此一欄位移至 superclass。

Motivation

去除,重複的資料宣告。
去除,重複的行為。

11.2 Pull Up Method

有些函式,在各個 subclass 中產生完全相同的結果。
將該函式移至 superclass

EX:

Salesman class => getName()
Engineer class => getName()

將 getName() => superclass (Employee class)

  • Class diagram
Motivation
  • 避免『行為重複』。

11.4 Push Down Method

將函式移至相關的 subclass 中。

Motivation

有必要將某些行為從 superclass 移至特定的subclass 時。

11.5 Push Down Field

將欄位移到需要它的 subclass 中。

11.6 Extract Subclass

新建一個 subclass,將部分特性移至 subclass 中。

Motivation

class 中的某些行為只被一部份的實體使用,其它實體不需要它們。

Extract Subclass 限制:
一旦物件創建完成,你無法再改變『與型別相關的行為』(class-based behavior)。

Extract Class / Extract Subclass 抉擇:
Extract Class (delegation) -> 數種不同方式變化。
Extract Subclass (inheritance) -> 一組變化。

11.10 Form Template Method

你有一些 subclass,其中相應的某些函數以相同順序執行類似的措施,
但各措施實際上有所不同。

  1. 將各個措施分別放進獨立的函式中,並保持它們都有相同的署名式(signature)。
  2. 將原函式上移至 superclass。

尚未完成…

文章目录
  1. 1. Target
  2. 2. Action Items
  3. 3. Quick Start
    1. 3.1. CH 11 Deal with Generalization
      1. 3.1.1. 11.1 Pull Up Field
        1. 3.1.1.1. Motivation
      2. 3.1.2. 11.2 Pull Up Method
        1. 3.1.2.1. EX:
        2. 3.1.2.2. Motivation
      3. 3.1.3. 11.4 Push Down Method
        1. 3.1.3.1. Motivation
      4. 3.1.4. 11.5 Push Down Field
      5. 3.1.5. 11.6 Extract Subclass
        1. 3.1.5.1. Motivation
      6. 3.1.6. 11.10 Form Template Method
  4. 4. 尚未完成…
,