l

2012年10月19日 星期五

尋找Force實驗5:Proxy篇

Oct. 17 08:30~:09:35

image

***

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

Intent

Provide a surrogate or placeholder for another object to control access to it.

上面這整句話「Provide a surrogate or placeholder for another object to control access to it」讀起來感覺像是solution,但是結尾的「control access to it」有點Problem的味道,就把這一句改寫成第一版的Problem。

Problem:How do you control access to an object?

中文:你如何控制對一個物件的存取?

接下來看一下GoF書中Proxy pattern的Motivation,看看能不能找到Force或是其他東西。

Motivation

One reason for controlling access to an object is to defer the full cost of its creation and initialization until we actually need to use it. Consider a document editor that can embed graphical objects in a document. Some graphical objects, like large raster images, can be expensive to create. But opening a document should be fast, so we should avoid creating all the expensive objects at once when the document is opened. This isn't necessary anyway, because not all of these objects will be visible in the document at the same time.

These constraints would suggest creating each expensive object on demand, which in this case occurs when an image becomes visible. But what do we put in the document in place of the image? And how can we hide the fact that the image is created on demand so that we don't complicate the editor's implementation? This optimization shouldn't impact the rendering and formatting code, for example.

上面這兩段文字中標示為藍色的字,說明了使用Proxy的理由:想要延後產生與初始化一個物件的成本,以便增加系統的效能(加速或是減少記憶體的使用量)。這裡可以找到三個Force。

Force 1: You have an object and its functional requirements are implemented completely.

Force 2: For non-functional reasons such as reducing memory usage and increasing operation performance, you may want to add cross-cutting concerns such as object creation, initialization, and protection to the object. Modifying the original object to implement the cross-cutting concerns violates general object-oriented design principles. 

Force 3: The object’s client should not know its internal implementation. Thus, whether the object is created on demand and located locally or remotely should be hidden from its client.

好像快把Proxy的Force給找完了,繼續看下去。

The solution is to use another object, an image proxy, that acts as a stand-in for the real image. The proxy acts just like the image and takes care of instantiating it when it's required.

The image proxy creates the real image only when the document editor asks it to display itself by invoking its Draw operation. The proxy forwards subsequent requests directly to the image. It must therefore keep a reference to the image after creating it.

Let's assume that images are stored in separate files…(後面全部省略).

以上內容都在講Solution的例子。

***

上面三個Force大致已經涵蓋Applicability所要敘述的內容,Applicability的內容就省略不列出來了,直接整理出最後的結果:

Problem: How do you control access to an object?

Force:

  • You have an object and its functional requirements are implemented completely.
  • For non-functional reasons such as reducing memory usage and increasing operation performance, you may want to add cross-cutting concerns such as object creation, initialization, and protection to the object. Modifying the original object to implement the cross-cutting concerns violates general object-oriented design principles.
  • For information hidden, the object’s client should not know its internal implementation. Thus, whether the object is created on demand and located locally or remotely should be hidden from its client.

***

友藏內心獨白:整理過後的Proxy有比較容易理解嗎?

沒有留言:

張貼留言