廖峻鋒 aug 22,2003 nccu computer center

47
UML-F tags for Framework Constr uction Principles and Patterns - The UML Profile for Framework Architectures 讀讀讀讀讀讀 廖廖廖 Aug 22,2003 NCCU Computer Center

Upload: mignon

Post on 22-Jan-2016

52 views

Category:

Documents


0 download

DESCRIPTION

UML-F tags for Framework Construction Principles and Patterns - The UML Profile for Framework Architectures 讀書會第四章. 廖峻鋒 Aug 22,2003 NCCU Computer Center. 學習目標. 建構 Framework 的二大原則 ( 及其標記 ) Unification / Separation Template-Hook 觀念 ( 及其標記 ) Gof Patterns 的五大建構原則 (Meta Patterns) ( 及其標記 ) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 廖峻鋒 Aug 22,2003 NCCU Computer Center

UML-F tags for Framework Construction Principles and Patterns

- The UML Profile for Framework Architectures 讀書會第四章

廖峻鋒Aug 22,2003NCCU Computer Center

Page 2: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

學習目標 建構 Framework 的二大原則 ( 及其標記 )

Unification / Separation Template-Hook 觀念 ( 及其標記 ) Gof Patterns 的五大建構原則 (Meta Patterns)

( 及其標記 ) 所有的 Gof 皆可歸為這五類。

如何自訂標記

Page 3: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Agenda 背景知識 Template / Hook Essential Framework Construction Principles UML-F for Gof Patterns UML-F for Domain-specific Patterns 進階閱讀參考

Page 4: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

背景知識 控制權互換 (Inversion of Control) Template / Hook 三個重要的基本 Patterns

Page 5: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

[ 重要 ]實作和流程控制角色的互換(Inversion of Control)

傳統的公用函式庫 (Library) 函式由函式庫定義。 我們寫主程式,在主程式中呼叫函式。 你控制流程, Library 提供實作。

框架 (Framework) 主程式由框架定義。 我們實作函式,被框架定義的主程式所呼叫。 Framework 控制流程,你提供實作 !

Page 6: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Inversion of Control

Page 7: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Library ReuseString countStr = “256”;int count = Integer.parseInt(countStr);

你控制流程, Library 提供實作 !

Structured Style!

Page 8: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Framework Reusepublic class MyServlet extends HttpServlet{ public void doGet(…){ // 提供實作 }}Framework 控制流程,你提供實作 !

Call Back Style!

Page 9: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Meta Patterns Gof Patterns 其實用來用去也都是那幾招。 由 Template / Hook 組合成不同 Meta Patterns. 作者認為各式各樣的 Design Patterns 只是 T / H的不同組合而已。

Page 10: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Template Method /Hook Method

我們寫 Framework 時主要邏輯通常會寫在 Template Method 中。

Template Method 會呼叫若干 Hook Method , Hook Method 通常就是「變異點」。

只要改寫 Hook Method ,就可改變 Template Method 的行為。 ( 課本第 68 頁中 )

參考下頁的例子。

Page 11: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

範例 :Template / Hook Method

Page 12: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Template/Hook Class Template Class /Hook Class

擁有 Template method 的類別稱為 Template Class 。 擁有 Hook method 的類別稱為 Hook Class 。

同一類別可能同時是 Template 及 Hook Class 。( 例如 :Unif. 、 Chain of Resp.)

Page 13: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

UML-F 層級關係

Page 14: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

UML-F (Template / Hook)

Page 15: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

範例類似 name space 觀念

可以想成 : 在做 s1 這件任務時, B 是擔任 hook class的角色,做 s2 時, B 是擔任 template class 的角色…

Page 16: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Hook Scale up Template Class 可能也是別人的 Hook Class 可參閱上一張投影片

Page 17: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

三個重要的基本 Patterns Delegate[Grand98] - 繼承的替代方案 Template Method[Gof95]- Inversion of Contro

l 的標準方式 ! Interface[Grand98] - 利用界面區隔類別使用者

(Client) 與類別實作 (Concrete Classes).

Page 18: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Delegate

Java API 中到處都是 Delegate.

- Patterns in Java ,volume I by Mark Grand.

Page 19: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Delegate 範例

觀察 :Design Pattern 時常混合出現 (A.Shalloway et al. 2000)

Page 20: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Interface[Grand98] Interface 規定了一組方法 (method) ,所有實作它的元件都要實作所有方法。

Client 呼叫的是 Interface 中的方法,所以元件的抽換對 Client 來說是感覺不到的 ( 不用改 code) 。

Page 21: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

實作界面就可保証符合規格

Page 22: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

java.io.FilenameFilterps. 其實這是一個 strategy pattern

Page 23: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Template Method[Gof95] 一個 Browser 向 HttpServlet 發出 post 時,會觸發 doPost() 方法。

一個 Browser 向 Servlet 發出 get 時,會觸發 doGet() 方法。

判別是 get 或 post ,由 HttpServlet 決定,至於 doPost() , doGet() 由子類別決定。

Template Method 是 Framework 中最常見到的Design Pattern!

Page 24: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Servlet

Page 25: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Patterns 的五大建構原則 根據本書的歸納,所有 Gof Patterns 可分為五大

Meta Patterns. Unification 、 Separation 、 Composite 、 Decorat

or 及 Chain-Of-Responsibility( 參見 p96,Fig 4.24) 。

Page 26: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Patterns 的五大建構原則

Page 27: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

五大原則 ( 圖 )

Unification Separation

觀察 :Template 在子類別 !

Page 28: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

建構框架二大基本原則-Essential Framework construction principles

• Unification construction principle• Separation construction principle

Page 29: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Unification

Template method

Hook method

template 、 hook 在同一個 Class

Page 30: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

範例 : 登入機制的抽象化

Page 31: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

UML-F (Unification)

UML-F tags for Unification Construction Principle

Page 32: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Separation

Template method

Hook method

Template Class 與 Hook Class 有委任關係

Observer Pattern

Page 33: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

範例 :StrategyDelegate

觀察 :Design Pattern 時常混合出現 (A.Shalloway et al. 2000)

Interface

Page 34: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

UML-F(Separation)

Page 35: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

範例 :FilenameFilter

Page 36: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Abstract Coupling( 課本 4.3.1) Separation 原則時常是 Abstract Coupling ,所以有時候我們也稱 Separation 為 Abstract Coupling 。

Page 37: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Abstract Coupling( 課本 4.3.1)

Abstract coupling

這是一個 Factory Method Pattern

Page 38: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

UML-F tags for Gof Patterns Factory Method Strategy Composite

這裏僅以 Facotry Method 為例,說明其方法。其它樣式請自行參考課本附錄 B!

Page 39: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Factory Method 的成員

Page 40: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

UML-F for Factory Method 類別 :

FacM-Creator FacM-Product FacM-ConcreteCreator FacM-ConcreteProduct

方法 FacM-facM,FacM-facM,FacM-anOp.

抽象方法 !

Page 41: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

範例 : java.net.ContentHandler

Page 42: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

UML-F tags for Domain-specific Patterns Calculation Pattern

其實這個 Pattern 就是一個標準的 Strategy Pattern. 將 Strategy 的 UML-F 標籤換成自訂的 Domain-Spec

ific tag.( 參考課本 p.108)

Page 43: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Framework 進階閱讀 Building Application Framework (Wiley)- 這本很像論文集,不好唸且錯誤很多,宜以讀書會方式加以了解。

Java Application Framework (Wiley). 應用架構入門與實例 – 台灣本土唯一一本 Fram

ework 著作,但是是數年前的著作,前幾章仍有閱讀的價值。 ( 可自 MISOO上自由下傳 ) 。

Page 44: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Design Patterns 學習地圖 精通任一種 OO 語言及 OO 基本觀念 ( 繼承封裝多形 ) 。 Thinking in Java( 有中譯本 ) .

Design Patterns 入門 Applying UML and Patterns: An Introduction to Obj

ect-Oriented Analysis, 2/e( 有中譯本 ) Design Patterns Explained: A New Perspective on

Object-Oriented Design( 有中譯本 ) Design Patterns 於 Java 語言上的實習應用 ( 有中譯本 )

Page 45: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Design Patterns 學習地圖 2 觀察別人如何使用 Patterns(Java)

Java Design Patterns: A Tutorial Applied Java Patterns( 有中譯本 ) Patterns in Java VI,V2.

活用 Patterns Design Patterns Java Workbook( 有中譯本 ) Pattern Hatching: Design Patterns Applied( 這本是以 C++ 為例 ).

Page 46: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Design Patterns 學習地圖 3 經典

Gof( 有中譯本 ) Architectural Patterns

Patterns of Enterprise Application Architecture Pattern Oriented Software Architecture (POSA)

• 這本書 scope 非常廣,包含了 Software Architecture 、 Architectural Patterns 、 Design Patterns 及 Idioms 。

Page 47: 廖峻鋒 Aug 22,2003 NCCU Computer Center

國立政治大學電子計算機中心

Design Patterns 學習地圖 4 HillSide Group(http://hillside.net) - 由 Kent B

eck 及 Grady Booch 建立,是 Pattern 族群的聖殿。