4. jjhou@jjhou.com http://www.jjhou.com http://jjhou.csdn.net 4
Bibliography
《Design Patterns 於Java語言㆖的實習應用》
by 結城 浩
http://www.hyuki.com/dp/index.html
http://www.hyuki.com/dp/dpsrc_2004-05-26.zip
採 The zlib/libpng License,不限制包括
商業應用在內的任何用途。
5. jjhou@jjhou.com http://www.jjhou.com http://jjhou.csdn.net 11
2. Adapter in GOF
Convert the interface of a class into another interface clients expect. Adapter lets
classes work together that couldn't otherwise because of incompatible interfaces.
轉換 class 的介面使其為 client 所期望。Adapter 使得原本因「介面不相容」
而無法合作的 classes 變得可以合作。
Target
SpecificRequest()
Adaptee
Request()
Client
Request()
Adapter
adaptee->SpecificRequest()
adaptee
Adapter 是為了復用那些「已經做過許多測試、臭蟲不多」的 classes。
也可用於軟件版本更迭之維護。Adapter 有兩種:
• Class Adapter(運用 inheritance 實現出來)
• Object Adapter(運用 delegation 實現出來)
14. jjhou@jjhou.com http://www.jjhou.com http://jjhou.csdn.net 76
21. Strategy (Policy) in GOF
Define a family of algorithms, encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from clients that use it.
定義㆒整個族系的演算法,將每㆒個封裝起來(成為 class)並使得以被交換使用。
Strategy 可使得演算法的抽換不影響其客戶。
algorithm()
《interface》
Strategy
Context
s : Strategy
Strategy A
algorithm()
Strategy B
algorithm()
Strategy C
algorithm()
18. jjhou@jjhou.com http://www.jjhou.com http://jjhou.csdn.net 80
22. Template Method in GOF
Define the skeleton of an algorithm in an operation, de'ferring some steps to
subclasses. Template Method lets subclasses redefine certain steps of an algorithm
without changing the algorithm's structure.
定義演算法骨幹,延緩其㆗某些步驟,使它們在subclasses ㆗才獲得真正定義。
Template Method 使 subclasses 得以重新定義演算法內的某些動作,而不需改
變演算法的總體結構。
TemplateMethod()
PrimitiveOperation1()
PrimitiveOperation2()
PrimitiveOperation1()
PrimitiveOperation2()
AbstractClass
ConcreteClass
…
PrimitiveOperation1()
…
PrimitiveOperation2()
...
• Template Method 是利用 inheritance 來改變程式行
為:在 base class 指定行事大綱,在 sub class 指定
具體行為。
• Strategy 是利用 delegation 來改變程式行為:改變
的不是演算法局部,而是切換整個演算法。
72. jjhou@jjhou.com http://www.jjhou.com http://jjhou.csdn.net 179
㆔層㆔層㆔層㆔層. Memory Deallocation
void ordered_free(void * const chunk)
{
// This (slower) implementation of 'free' places
// the memoryback in the list in its proper order.
// Find where "chunk" goes in the free list
void * const loc = find_prev(chunk);
// Place either at beginning or in middle/end
if (loc == 0)
free(chunk);
else {
nextof(chunk) = nextof(loc);
nextof(loc) = chunk;
}
}
void ordered_free(void * const chunk)
{
// This (slower) implementation of 'free' places
// the memoryback in the list in its proper order.
// Find where "chunk" goes in the free list
void * const loc = find_prev(chunk);
// Place either at beginning or in middle/end
if (loc == 0)
free(chunk);
else {
nextof(chunk) = nextof(loc);
nextof(loc) = chunk;
}
}
find_prev(void * const ptr)
{ // Handle border case
if (first == 0 || std::greater<void *>()(first, ptr))
return 0;
void * iter = first;
while (true) {
// if we're about to hit the end or
// if we've found where "ptr" goes
if (nextof(iter) == 0 ||
std::greater<void *>()(nextof(iter), ptr))
return iter;
iter = nextof(iter);
}
}
find_prev(void * const ptr)
{ // Handle border case
if (first == 0 || std::greater<void *>()(first, ptr))
return 0;
void * iter = first;
while (true) {
// if we're about to hit the end or
// if we've found where "ptr" goes
if (nextof(iter) == 0 ||
std::greater<void *>()(nextof(iter), ptr))
return iter;
iter = nextof(iter);
}
}
void free(void * const chunk)
{
nextof(chunk) = first;
first = chunk;
}
void free(void * const chunk)
{
nextof(chunk) = first;
first = chunk;
}
如果目前有㆒個 block,已分配 5 chunks 出去,
first 指向 6th chunk:
264 64 8
... 0 0
那麼當歸還 2rd chunk 指標時:
264 64 8
... 0 0
歸還 4th chunk 指標時:
264 64 8
... 0 0
p2 p4
p4
iter 最後停在這裡,
傳回被 loc 承接
滿足這個條件
first
first
first
這個迴圈將找出first之後
的㆒個 "適當" 位置
滿足這個條件
這個algorithm㆒定和
"blocks 的串接永遠從低
位址至高位址排列" 有關
73. jjhou@jjhou.com http://www.jjhou.com http://jjhou.csdn.net 180
㆔層㆔層㆔層㆔層. Memory Deallocation
void ordered_free(void * const chunk)
{
// This (slower) implementation of 'free' places
// the memoryback in the list in its proper order.
// Find where "chunk" goes in the free list
void * const loc = find_prev(chunk);
// Place either at beginning or in middle/end
if (loc == 0)
free(chunk);
else {
nextof(chunk) = nextof(loc);
nextof(loc) = chunk;
}
}
void ordered_free(void * const chunk)
{
// This (slower) implementation of 'free' places
// the memoryback in the list in its proper order.
// Find where "chunk" goes in the free list
void * const loc = find_prev(chunk);
// Place either at beginning or in middle/end
if (loc == 0)
free(chunk);
else {
nextof(chunk) = nextof(loc);
nextof(loc) = chunk;
}
}
find_prev(void * const ptr)
{ // Handle border case
if (first == 0 || std::greater<void *>()(first, ptr))
return 0;
void * iter = first;
while (true) {
// if we're about to hit the end or
// if we've found where "ptr" goes
if (nextof(iter) == 0 ||
std::greater<void *>()(nextof(iter), ptr))
return iter;
iter = nextof(iter);
}
}
find_prev(void * const ptr)
{ // Handle border case
if (first == 0 || std::greater<void *>()(first, ptr))
return 0;
void * iter = first;
while (true) {
// if we're about to hit the end or
// if we've found where "ptr" goes
if (nextof(iter) == 0 ||
std::greater<void *>()(nextof(iter), ptr))
return iter;
iter = nextof(iter);
}
}
void free(void * const chunk)
{
nextof(chunk) = first;
first = chunk;
}
void free(void * const chunk)
{
nextof(chunk) = first;
first = chunk;
}
如果目前有 3 個 blocks 如㆘:
1032 256 8
...
32個小區塊
0 0
...
64個小區塊
264
...
128個小區塊
520
007E0250
007E0490
007D09A4
0
p4
歸還 p4 會變成這樣
(見㆘頁)
iter 最後停在這裡,
傳回被 loc 承接