SlideShare a Scribd company logo
1 of 21
Ch4. The Factory Pattern
Head First Design Patterns
2014/04/24
vicall@Qlync Inc.
1
OO Principles - ch1~3
Encapsulate what varies.
Favor composition(合成) over inheritance(繼承)
Program to interface, not implementations.
Strive for loosely coupled designs between objects
that interact.
Classes should be open for extension but closed for
modification.
2
NOT Closed for Modification
3
–Ch3. The Decorator Pattern
“Open for Extension
Closed for Modification”
4
⼯工廠模式
所有⼯工廠都是⽤用來封裝物件的建⽴立。
所有的⼯工廠模式都提倡鬆綁,以便降低應⽤用程式和具
象類別之間的相依程度。
5
簡單⼯工廠
簡單⼯工廠,雖然不是真正的設計模式,但仍不失為⼀一
個簡單的⽅方法,可以將程式鬆綁於具像類別之外。
6
Sample Factory
Extend PizzaStore……
7
–Ch2. The Observer Pattern
“Strive for loosely coupled designs between
objects that interact.”
8
⼯工廠⽅方法模式
⼯工廠⽅方法使⽤用到繼承:把物件的建⽴立委託給次類別進
⾏行,次類別實踐⼯工廠⽅方法以建⽴立物件。
⼯工廠⽅方法允許類別將實體化的動作,交由次類別進
⾏行。
9
The Factory Method Pattern defines an interface for creating
an object, but lets subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation to subclasses.
10
11
–Ch4. The Factory Pattern
[ Dependency Inversion Principle ]
顛覆依賴守則
!
“Depend upon abstractions. Do not depend upon
concrete classes.”
依賴抽象類別,不要依賴具象類別。
12
What’s Good?
封裝變動
避免重複
⽅方便修改
針對界⾯面
13
抽象⼯工廠模式
抽象⼯工廠使⽤用物件合成:物件的建⽴立被實踐在⼯工廠介
⾯面所暴露出來的⽅方法中。
抽象⼯工廠建⽴立相關的物件家族,⽽而不需要依賴他們的
具象類別。
14
The Abstract Factory Pattern provides an interface for
creating families of related or dependent objects without
specifying their concrete classes.
15
16
⼯工廠模式
所有⼯工廠都是⽤用來封裝物件的建⽴立。
所有的⼯工廠模式都提倡鬆綁,以便降低應⽤用程式和具
象類別之間的相依程度。
17
⼯工廠⽅方法模式
⼯工廠⽅方法使⽤用到繼承:把物件的建⽴立委託給次類別進
⾏行,次類別實踐⼯工廠⽅方法以建⽴立物件。
⼯工廠⽅方法允許類別將實體化的動作,交由次類別進
⾏行。
18
抽象⼯工廠模式
抽象⼯工廠使⽤用物件合成:物件的建⽴立被實踐在⼯工廠介
⾯面所暴露出來的⽅方法中。
抽象⼯工廠建⽴立相關的物件家族,⽽而不需要依賴他們的
具象類別。
19
OO Principles - ch1~4
Encapsulate what varies.
Favor composition(合成) over inheritance(繼承)
Program to interface, not implementations.
Strive for loosely coupled designs between objects that interact.
Classes should be open for extension but closed for modification.
Depend upon abstractions. Do not depend upon concrete
classes.
20
Thanks!
!
Discussion…
21

More Related Content

Viewers also liked

Cs 1023 lec 9 design pattern (week 2)
Cs 1023 lec 9 design pattern (week 2)Cs 1023 lec 9 design pattern (week 2)
Cs 1023 lec 9 design pattern (week 2)stanbridge
 
Factory Pattern in Luster
Factory Pattern in LusterFactory Pattern in Luster
Factory Pattern in LusterJason Chung
 
JCConf TW 2014 - Modern Design Pattern
JCConf TW 2014 - Modern Design PatternJCConf TW 2014 - Modern Design Pattern
JCConf TW 2014 - Modern Design PatternSteven Wang
 
Design Patterns in Luster
Design Patterns in LusterDesign Patterns in Luster
Design Patterns in LusterJason Chung
 
模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式teddysoft
 
User experience service design innovation for real world
User experience service design innovation for real worldUser experience service design innovation for real world
User experience service design innovation for real worldNTUST
 

Viewers also liked (8)

Factory Patterns
Factory PatternsFactory Patterns
Factory Patterns
 
Cs 1023 lec 9 design pattern (week 2)
Cs 1023 lec 9 design pattern (week 2)Cs 1023 lec 9 design pattern (week 2)
Cs 1023 lec 9 design pattern (week 2)
 
Factory Pattern in Luster
Factory Pattern in LusterFactory Pattern in Luster
Factory Pattern in Luster
 
JCConf TW 2014 - Modern Design Pattern
JCConf TW 2014 - Modern Design PatternJCConf TW 2014 - Modern Design Pattern
JCConf TW 2014 - Modern Design Pattern
 
Design Patterns in Luster
Design Patterns in LusterDesign Patterns in Luster
Design Patterns in Luster
 
模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式模式入門第一堂課: 30分鐘寫出一個模式
模式入門第一堂課: 30分鐘寫出一個模式
 
User experience service design innovation for real world
User experience service design innovation for real worldUser experience service design innovation for real world
User experience service design innovation for real world
 
What's New with AWS Lambda
What's New with AWS LambdaWhat's New with AWS Lambda
What's New with AWS Lambda
 

Similar to Design Pattern - Factory Pattern

软件工程 第五章
软件工程 第五章软件工程 第五章
软件工程 第五章浒 刘
 
腾讯大讲堂05 面向对象应对之道
腾讯大讲堂05 面向对象应对之道腾讯大讲堂05 面向对象应对之道
腾讯大讲堂05 面向对象应对之道topgeek
 
腾讯大讲堂05 面向对象应对之道
腾讯大讲堂05 面向对象应对之道腾讯大讲堂05 面向对象应对之道
腾讯大讲堂05 面向对象应对之道areyouok
 
安卓中的设计模式举例 by hjm1fb
安卓中的设计模式举例 by hjm1fb安卓中的设计模式举例 by hjm1fb
安卓中的设计模式举例 by hjm1fbAlbert Hong
 
设计模式-单例、享元、工厂与抽象工厂
设计模式-单例、享元、工厂与抽象工厂设计模式-单例、享元、工厂与抽象工厂
设计模式-单例、享元、工厂与抽象工厂诸葛修车网-诸葛商城
 
掌星 移动互联网开发笔记-Vol001
掌星 移动互联网开发笔记-Vol001掌星 移动互联网开发笔记-Vol001
掌星 移动互联网开发笔记-Vol001rainx1982
 
Refactoring with Patterns in PHP
Refactoring with Patterns in PHPRefactoring with Patterns in PHP
Refactoring with Patterns in PHPJace Ju
 
Design Method_20190522
Design Method_20190522Design Method_20190522
Design Method_20190522Winny Wang
 
Clipper@datacon.2019.tw
Clipper@datacon.2019.twClipper@datacon.2019.tw
Clipper@datacon.2019.twWei-Yu Chen
 
2012/05/23 AU Talk - 讓事情發生
2012/05/23 AU Talk - 讓事情發生2012/05/23 AU Talk - 讓事情發生
2012/05/23 AU Talk - 讓事情發生appuniverz
 
TDD (Test-driven development, 測試驅動開發) 基本教學
TDD (Test-driven development, 測試驅動開發) 基本教學TDD (Test-driven development, 測試驅動開發) 基本教學
TDD (Test-driven development, 測試驅動開發) 基本教學潘 冠辰
 
软件工程 第十一章
软件工程 第十一章软件工程 第十一章
软件工程 第十一章浒 刘
 

Similar to Design Pattern - Factory Pattern (18)

软件工程 第五章
软件工程 第五章软件工程 第五章
软件工程 第五章
 
腾讯大讲堂05 面向对象应对之道
腾讯大讲堂05 面向对象应对之道腾讯大讲堂05 面向对象应对之道
腾讯大讲堂05 面向对象应对之道
 
腾讯大讲堂05 面向对象应对之道
腾讯大讲堂05 面向对象应对之道腾讯大讲堂05 面向对象应对之道
腾讯大讲堂05 面向对象应对之道
 
安卓中的设计模式举例 by hjm1fb
安卓中的设计模式举例 by hjm1fb安卓中的设计模式举例 by hjm1fb
安卓中的设计模式举例 by hjm1fb
 
设计模式-单例、享元、工厂与抽象工厂
设计模式-单例、享元、工厂与抽象工厂设计模式-单例、享元、工厂与抽象工厂
设计模式-单例、享元、工厂与抽象工厂
 
111
111111
111
 
111
111111
111
 
111
111111
111
 
111
111111
111
 
111
111111
111
 
掌星 移动互联网开发笔记-Vol001
掌星 移动互联网开发笔记-Vol001掌星 移动互联网开发笔记-Vol001
掌星 移动互联网开发笔记-Vol001
 
Refactoring with Patterns in PHP
Refactoring with Patterns in PHPRefactoring with Patterns in PHP
Refactoring with Patterns in PHP
 
面向对象设计原则
面向对象设计原则面向对象设计原则
面向对象设计原则
 
Design Method_20190522
Design Method_20190522Design Method_20190522
Design Method_20190522
 
Clipper@datacon.2019.tw
Clipper@datacon.2019.twClipper@datacon.2019.tw
Clipper@datacon.2019.tw
 
2012/05/23 AU Talk - 讓事情發生
2012/05/23 AU Talk - 讓事情發生2012/05/23 AU Talk - 讓事情發生
2012/05/23 AU Talk - 讓事情發生
 
TDD (Test-driven development, 測試驅動開發) 基本教學
TDD (Test-driven development, 測試驅動開發) 基本教學TDD (Test-driven development, 測試驅動開發) 基本教學
TDD (Test-driven development, 測試驅動開發) 基本教學
 
软件工程 第十一章
软件工程 第十一章软件工程 第十一章
软件工程 第十一章
 

More from Li-Wei Yao

SSL Certificate and Code Signing
SSL Certificate and Code SigningSSL Certificate and Code Signing
SSL Certificate and Code SigningLi-Wei Yao
 
From NAT to NAT Traversal
From NAT to NAT TraversalFrom NAT to NAT Traversal
From NAT to NAT TraversalLi-Wei Yao
 
6.2 Move Creation Knowledge to Factory - Refactoring to Patterns
6.2 Move Creation Knowledge to Factory - Refactoring to Patterns6.2 Move Creation Knowledge to Factory - Refactoring to Patterns
6.2 Move Creation Knowledge to Factory - Refactoring to PatternsLi-Wei Yao
 
6.3 Encapsulate Classes with Factory - Refactoring to Patterns
6.3 Encapsulate Classes with Factory - Refactoring to Patterns6.3 Encapsulate Classes with Factory - Refactoring to Patterns
6.3 Encapsulate Classes with Factory - Refactoring to PatternsLi-Wei Yao
 
6.1 Replace Constructors with Creation Methods - Refactoring to Patterns
6.1 Replace Constructors with Creation Methods - Refactoring to Patterns6.1 Replace Constructors with Creation Methods - Refactoring to Patterns
6.1 Replace Constructors with Creation Methods - Refactoring to PatternsLi-Wei Yao
 
Qlync RD 第三屆讀書會候選清單
Qlync RD 第三屆讀書會候選清單Qlync RD 第三屆讀書會候選清單
Qlync RD 第三屆讀書會候選清單Li-Wei Yao
 
自動測試 - 測試對象的識別/封裝
自動測試 - 測試對象的識別/封裝自動測試 - 測試對象的識別/封裝
自動測試 - 測試對象的識別/封裝Li-Wei Yao
 
Design Pattern - Iterator and Composite Patterns
Design Pattern - Iterator and Composite PatternsDesign Pattern - Iterator and Composite Patterns
Design Pattern - Iterator and Composite PatternsLi-Wei Yao
 

More from Li-Wei Yao (9)

SSL Certificate and Code Signing
SSL Certificate and Code SigningSSL Certificate and Code Signing
SSL Certificate and Code Signing
 
From NAT to NAT Traversal
From NAT to NAT TraversalFrom NAT to NAT Traversal
From NAT to NAT Traversal
 
Docker Usage
Docker UsageDocker Usage
Docker Usage
 
6.2 Move Creation Knowledge to Factory - Refactoring to Patterns
6.2 Move Creation Knowledge to Factory - Refactoring to Patterns6.2 Move Creation Knowledge to Factory - Refactoring to Patterns
6.2 Move Creation Knowledge to Factory - Refactoring to Patterns
 
6.3 Encapsulate Classes with Factory - Refactoring to Patterns
6.3 Encapsulate Classes with Factory - Refactoring to Patterns6.3 Encapsulate Classes with Factory - Refactoring to Patterns
6.3 Encapsulate Classes with Factory - Refactoring to Patterns
 
6.1 Replace Constructors with Creation Methods - Refactoring to Patterns
6.1 Replace Constructors with Creation Methods - Refactoring to Patterns6.1 Replace Constructors with Creation Methods - Refactoring to Patterns
6.1 Replace Constructors with Creation Methods - Refactoring to Patterns
 
Qlync RD 第三屆讀書會候選清單
Qlync RD 第三屆讀書會候選清單Qlync RD 第三屆讀書會候選清單
Qlync RD 第三屆讀書會候選清單
 
自動測試 - 測試對象的識別/封裝
自動測試 - 測試對象的識別/封裝自動測試 - 測試對象的識別/封裝
自動測試 - 測試對象的識別/封裝
 
Design Pattern - Iterator and Composite Patterns
Design Pattern - Iterator and Composite PatternsDesign Pattern - Iterator and Composite Patterns
Design Pattern - Iterator and Composite Patterns
 

Design Pattern - Factory Pattern