l

2013年3月6日 星期三

Behavioral Patterns要解決什麼問題(一)?

Mar. 04 16:32~18:10
image
去年談了「Creational Patterns要解決什麼問題」(請參考《上集》、《中集》、《下集》)與「Structural Patterns要解決什麼問題」(請參考《上集》),今天終於輪到behavioral pattern要解決的問題。在GoF書中,屬於behavioral pattern這個分類模式的有11個,是三個分類中設計模式數量最多的一個。在逐一閱讀每一個behavioral pattern之前,同樣地先請鄉民們思考以下兩個問題:
  1. 這11個behavioral pattern到底是要解決什麼問題?
  2. 它們之間的差別是什麼?
Intent
首先看一下GoF書中對於這11個behavioral pattern的Intent說明:
  • Chain of Responsibility: Avoid coupling the sender of a request to its receiver by giving more than one objet a change to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
  • Command: Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
  • Interpreter: Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language.
  • Iterator: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  • Mediator: Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
  • Memento: Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.
  • Observer: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
  • State: Allow an object to alter its behavior when tis internal state changes. The object will appear to change its class.
  • Strategy: Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
  • Template Method: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
  • Visitor: Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.
GoF書中每一個設計模式的Intent敘述其實都蠻像是Solution的簡短描述,因此要找出這11個pattern所要解決的共同問題,就不能直接觀察Intent的內容。在GoF的書中,有幫每一類的pattern做了一些分析比較,請翻開GoF第221頁的第一段:
Behavior patterns are concerned with algorithms and the assignment of responsibilities between objects…These patterns characterize complex control flow that’s difficult to follow at run-time.
在整理GoF這三個設計模式分類所要解決問題的過程,Teddy覺得behavioral pattern的問題比較不好描述。從上面這兩個句子可以改寫出三個版本:
  1. How do you assign responsibilities between objects?
  2. How do you assign responsibilities between objects to manage complex control flow?
  3. How do you assign responsibilities between objects to manage complex control flow that is difficult to follow at run-time?
鄉民:啊你嘛幫幫忙,上面這三個版本不是差不多?第3個版本的描述感覺比較完整,選這個好了。
Teddy:單獨看句子第3個版本的確是比較完整,但是「to manage control flow」與「difficult to follow at run-time」這兩個因素如果擺在Context之中,那麼就可以用比較簡短的句子「How do you assign algorithms and responsibilities between objects?」來描述問題。Teddy個人是習慣用簡短的句子來描述一個模式所要解決的問題,所以內心的小惡魔是想要選擇第1個版本,但說實話又覺得第3個版本對問題的描述比較完整,所以無法決定要採用何者較佳。那就採取折衷方案暫時採用第2個版本好了挑眉質疑
Problem:How do you assign responsibilities between objects to manage complex control flow?
***
這個描述一個一般化且共通的問題,可以適用於這11個behavioral pattern。但是,光是這樣不足以區隔這11個pattern,也就無法推導出針對這11個pattern要採用哪種特定的solution。請問鄉民,如果這11個pattern所要解決的問題都一樣,要如何區隔它們?如果有看過這系列文章的鄉民,這個問題的答案應該很清楚了:從個別pattern的Force可以演化出不同的Solution。
螢幕快照 2013-03-04 下午5.46.22
***
今天先寫到這裡,下回再慢慢介紹每一個behavioral pattern的force。
***
友藏內心獨白:設計模式這個主題再不灌溉一下都快要枯萎凋零了。

4 則留言:

  1. Structural Patterns要解決什麼問題,下集的連結到〈Creational Patterns要解決什麼問題(下)?〉

    回覆刪除
  2. Hi sam,

    我剛剛才發現我根本沒寫『Structural Patterns要解決什麼問題,下集』...冏

    回覆刪除
  3. 無怪乎,繞了好久,紿終找不到XD

    回覆刪除
  4. Hi sam,

    這一系列荒廢太久,久到自己都忘了寫到哪裡了...Orz...

    回覆刪除