SlideShare a Scribd company logo
6.1.
Replace Constructors with Creation
Methods
6. Creation(6.1)
11. Utilities
YAO, LI-WEI
2014/08/14
Qlync Inc., RD Reading
以『⽬目的清楚,返回物件實體』的Creation Method
取代建構式
Motivation
建構式(Constructors)
名稱限制,複數同名建構式,學習使⽤用不便。︒
易誤⽤用建構式,無法傳達物件先天性質。︒
建構式受限於簽名(signature)無法重複。︒
無使⽤用的建構式未適時清除。︒
Motivation
Creation Method
無名稱限制,可清楚表達。︒
可接受各式引數(不限數量及型別)。︒
名稱不同⽅方便使⽤用及維護
Pros & Cons
(+)比建構式(constructors)更能有效表達可獲得哪⼀一
種物件實體。︒
(+)突破建構式的限制,像是不能同時擁有兩個『引數
個數和引數型別均相同』的建構式。︒
(+)更容易找出未使⽤用的建構碼(creation code)。︒
(−)創建(creation)⽅方式變得不標準:有些classes使⽤用
new來具現化(instantiated),有些使⽤用Creation Method。︒
Refactoring [F]
Extract Method [110]
Inline Method [117]
Move Method [142]
Introduce Parameter Object [295]
Extract Method [110]
Inline Method [117]
Move Method [142]
Introduce Parameter Object [295]
Mechanics
1. Find out “cache-all” constructer “Ctor1"
1.1. Extract “Creation Method”
1.2. Move “Creation Method”
2. Replace “Ctor1” with “Creation Method”
3. “Inline Method”, if another constructer “Ctor2” called by “Ctor1”
4. Repeat 1~3
5. Visibility, private the “cache-all” constructer “Ctor1” if no external caller
11
Example in Book…
Variations
太多Creation Method
Parameterized Creation
Methods
Extract Factory
—>
11. Utilities
11.1. Chain Constructors
11.2. Unify Interfaces
11.3. Extract Parameter
11.1. Chain Constructors
你⼿手上有多個內含重複碼
的建構式。︒
把建構式鏈結(chain)起
來,以獲得最少量重複碼。︒
11.2. Unify Interfaces
你需要讓⼀一個superclass或
interface擁有與subclass相同
的介⾯面。︒
在subclass身上找出
subclass/interface缺少的所
有public函式。︒把這些遺漏
的函式加至superclass並令
他們執⾏行null⾏行為。︒
11.3. Extract Parameter
函式或建構式以區域內具
現值賦予某個欄位
賦予此欄位⼀一個由客⼾戶提
供的參數,做法是將賦值
述句的⼀一部分提煉為參數。︒

More Related Content

PPTX
Improving Data Modeling Workflow
PPTX
Creating a Single Source of Truth: Leverage all of your data with powerful an...
PDF
2014 Mobile internet online user analysis report (TD)
PDF
豆瓣数据架构实践
PDF
Growth Hacking with Data: How to Find Big Growth with Deep Data Dives
PDF
唯品会大数据实践 Sacc pub
PDF
The State of Sales & Marketing at the 50 Fastest-Growing B2B Companies
PDF
The Future of Everything
Improving Data Modeling Workflow
Creating a Single Source of Truth: Leverage all of your data with powerful an...
2014 Mobile internet online user analysis report (TD)
豆瓣数据架构实践
Growth Hacking with Data: How to Find Big Growth with Deep Data Dives
唯品会大数据实践 Sacc pub
The State of Sales & Marketing at the 50 Fastest-Growing B2B Companies
The Future of Everything

More from Li-Wei Yao (9)

PDF
SSL Certificate and Code Signing
PDF
From NAT to NAT Traversal
PDF
Docker Usage
PDF
6.2 Move Creation Knowledge to Factory - Refactoring to Patterns
PDF
6.3 Encapsulate Classes with Factory - Refactoring to Patterns
PDF
Qlync RD 第三屆讀書會候選清單
PDF
自動測試 - 測試對象的識別/封裝
PDF
Design Pattern - Iterator and Composite Patterns
PDF
Design Pattern - Factory Pattern
SSL Certificate and Code Signing
From NAT to NAT Traversal
Docker Usage
6.2 Move Creation Knowledge to Factory - Refactoring to Patterns
6.3 Encapsulate Classes with Factory - Refactoring to Patterns
Qlync RD 第三屆讀書會候選清單
自動測試 - 測試對象的識別/封裝
Design Pattern - Iterator and Composite Patterns
Design Pattern - Factory Pattern
Ad

6.1 Replace Constructors with Creation Methods - Refactoring to Patterns