l

2012年10月3日 星期三

尋找Force實驗3:Adapter Pattern篇

Oct. 01 21:48~22:56

image

只找到延長線的圖片啊…XD。

 

今天來找Adapter pattern的force,依照慣例先把Intent改成Problem。

Intent

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

第一句話「Convert the interface of a class into another interface clients expect」感覺像是solution的一部分,第二句話「Adapter lets classes work together that couldn't otherwise because of incompatible interfaces」有一點點force的味道--因為介面不相容而導致物件無法一起使用(想要用某個物件,功能上都合用,但是就是因為介面不相容而無法直接使用,殘念)。

奇怪了,Adapter的Problem倒底是什麼?難道是如何解決物件介面不相容的問題嗎?感覺從Intent好像找不到Problem耶,怎麼辦?繼續往Motivation找看看。接下來看一下GoF書中Adapter pattern的Motivation。

Motivation

Sometimes a toolkit class that's designed for reuse isn't reusable only because its interface doesn't match the domain-specific interface an application requires.

第一句先停一下,請看reuse這個關鍵字,Teddy覺得Adapter的Problem可以寫成:

修改後:How can an existing class be reused? 

中文:一個既有的類別要如能夠被重複使用?

現在Problem有了(先姑且不論這個Problem寫得好不好),繼續往下找force,。

Consider for example a drawing editor that lets users draw and arrange graphical elements (lines, polygons, text, etc.) into pictures and diagrams. The drawing editor's key abstraction is the graphical object…(省略)

Classes for elementary geometric shapes like LineShape and PolygonShape are rather easy to implement, because their drawing and editing capabilities are inherently limited. But a TextShape subclass…(省略)

以上這兩段都在舉例子。

How can existing and unrelated classes like TextView work in an application that expects classes with a different and incompatible interface? We could change the TextView class so that it conforms to the Shape interface, but that isn't an option unless we have the toolkit's source code. Even if we did, it wouldn't make sense to change TextView; the toolkit shouldn't have to adopt domain-specific interfaces just to make one application work.

這一段的第一句整理一下,感覺也有點像是Problem,先整理起來放著:

How can existing and unrelated classes work in an application that expects classes with a different and incompatible interface?

繼續往下看,下面的這些內容其實已經在講Solution了。。

Instead, we could define TextShape so that it adapts the TextView interface to Shape's. We can do this in one of two ways: (1) by inheriting Shape's interface and TextView's implementation or (2) by composing a TextView instance within a TextShape and implementing TextShape in terms of TextView's interface…(省略)

This diagram illustrates the object adapter case…(省略)

Often the adapter is responsible for functionality the adapted class doesn't provide…(省略)

Manipulator is an abstract class for objects that know how to animate a Shape in response to user input, like dragging the shape to a new location…(省略)

***

最後看一下Applicability。

Applicability

  • you want to use an existing class, and its interface does not match the one you need.
  • you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.
  • (object adapter only) you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.

***

今天一天之內寫了三篇有點累了…Orz,推敲過程省略,直接看Teddy所找出的force,在這之前先把剛剛寫出的兩個Problem候選人列出來一下:

Problem:

  • How can an existing class be reused?
  • How can existing and unrelated classes work in an application that expects classes with a different and incompatible interface?

Force:

  • The interface of the existing class does not match the one you need.
  • You want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces.
  • You need to reuse several existing subclasses.

***

看完Force之後(感覺好像是把Applicability直接改成Force而已啊 XD),Teddy覺得用「How can an existing class be reused」當成Problem比較好。為什麼?兩個原因:

  • Teddy個人的偏好,比較喜歡把問題寫的簡短一點。
  • 從Force中其實已經點出「unrelated classes 」與「incompatible interface」這兩個限制,所以雖然如果把問題寫成「How can existing and unrelated classes work in an application that expects classes with a different and incompatible interface?」單獨來看這個問題會比「How can an existing class be reused? 」要清楚,但是如果搭配Force一起看,Teddy覺得把問題寫成簡短一點就可以了,除了可以凸顯最核心的問題之外(如何重複使用一個既存的類別),也比較不會出現敘述重複的現象(Problem和Force都重複出現)。

***

Teddy在這邊要先特別聲明一點,這一系列的文章,只是Teddy目前初步整理的結果,只代表Teddy個人的看法,不代表什麼「正確答案」。如果鄉民們有任何建議也請一併提供給Teddy參考,感恩。

***

友藏內心獨白:這麼偏門的內容,鄉民們通常都是不會有什麼建議滴 XD。

沒有留言:

張貼留言