• Part 1: 什麼是重構?
• Part 2: 從定義看重構
• Part 3: 重構的定位
– Refactoring and Patterns
• Part 4: 重構與設計方法
– TDD/BDD/SBE vs OOAD vs DDD
• Part 5: 重構範例討論
Copyright@2020 Teddysoft
• Refactoring (Noun )
– A change made to the internal structure of
software to make it easier to understand and
cheaper to modify without changing its observable
behavior. (對軟體內部結構的一種改變,目的在不改動軟體可察覺行
為的前提之下,提高其可理解性並降低修改成本。)
• Refactor (Verb)
– To restructure software by applying a series of
refactoring without changing its observable
behavior. (在不改變軟體可察覺行為的前提之下使用一系列重構方法
調整軟體結構 。)
Copyright@2020 Teddysoft Source:《Refactoring》
• 開發的兩頂帽子
– 實作功能性需求 (do the right thing)
– 改善設計(do the thing right; 寫出clean code)
• 呼應軟體設計原則
– Separation of Concerns
– Single Responsibility Principle
– One Pattern at a Time
Copyright@2020 Teddysoft
A change made to the internal structure
of software to make it easier to
understand and cheaper to modify
without changing its observable behavior.
較易理解,較低修改成本
Copyright@2020 Teddysoft
Understandability
Modifiability
Low Coupling
High Cohesion
OO Concept
OO Principle
Design Pattern
Architecture
⚫ Abstraction
⚫ Information hiding
⚫ Polymorphism
⚫ Responsibility
assignment
⚫ Indirection
⚫ Separation of concerns
⚫ Program to an interface,
not an implementation
⚫ Favor object composition
over class inheritance
⚫ Single-responsibility
⚫ Open-closed
⚫ Liskov substitution
⚫ Interface-segregation
⚫ Dependency-inversion
⚫ Don't repeat yourself,
Single choice, One and
only once
Copyright@2020 Teddysoft
• Make you instant expert
• Eliminate the need for intelligence and
taste
• Eliminate the need to think
• Tell you how exactly how to adapt the
solution to your context
Copyright@2020 Teddysoft
Source: Asian PLoP 2016 by Joseph
Yoder and Rebecca Wirfs-Brock
Copyright@2020 Teddysoft
重構 相反重構
Extract Method Inline Method
Add Parameter Remove Parameter
Collapse Hierarchy Extract Superclass, Extract Subclass
Extract Class Inline Class
Hide Delegate Remove Middle Man
Pull Up Field Push Down Field
Replace Inheritance with Delegation Replace Delegation with Inheritance