2013年11月13日 星期三

POSD學習筆記(8)-Template Method

先在base class裡面定義要/不要做的一連串動作,然後把動作設成abstract,由derived class決定每個abstract method動作的內容

Ex:
class base{
         final void template(){
                   A();
                   B();
                   C();
         }
         abstract void A();
         abstract void B();
         abstract void C();
}

class derived : base{
         public void A();
         public void B();
         public void C();
}

2013年11月12日 星期二

POSD學習筆記(7)-Strategy Pattern

沒有時間,先用學校投影片的圖擋一下,之後再自己畫

例:
Client new Context工具,然後呼叫Context.ContextInterface(Algorithm Type)
接著Context會讓Strategy去依照給的Algorithm Type去呼叫對應的ConcreteStrategy然後回傳給Context,Context再給Client。

好處:
  1. 同類型的演算法可歸為同一類
  2. 可以不用switch/if去選擇演算法











以下為轉載,轉自:http://www.cnblogs.com/oomusou/archive/2007/03/26/687727.html

Remark:
strategy和template method目的相同,皆對『新需求』的不同演算法提供『擴充』的機制,但手法卻不同,strategy採用object的方式,利用delegation改變演算法,而template method則採用class的繼承方式來改變演算法,也因為strategy採用object方式,所以有run-time改變的可能,但template method採class手法,所以無法run-time改變。

GoF的原文如下:
Template methods use inheritance to vary part of an algorithm. Strategies use delegation to vary the entire algorithm.




POSD學習筆記(6)-Composite Pattern & Decorator Pattern

Composite Pattern的作用:
  1. 階層式架構
  2. 使用者使用容易,不必在乎何者是leaf物件、何者是composite物件
  3. 容易新增物件,直接加掛即可
  4. 容易閱讀

老師教的Composite Pattern和Decorator Pattern的差別:
  • Decorator:adds additional responsibilities.
    • avoid explosion of subclassing
  • Composite:object aggregation.
    • treats one and multiple objects uniformly

網路上找到的Composite Pattern和Decorator Pattern的差別:
  • Composite Pattern:So the essence is that all elements in your composite structure have the same interface even though some are leaf nodes and others are entire structures. User interfaces often use this approach to allow easy composability.
    • 新增架構方便,使用者無感 。
    • (例如原本的圖只有圓跟方,現在又新增了三角形,而使用者可以直接去使用三角形,並不會有不便)

  • Decorator Pattern:The decorator pattern allows an entity to completely contain another entity so that using the decorator looks identical to the contained entity. This allows the decorator to modify the behaviour and/or content of whatever it is encapsulating without changing the outward appearance of the entity. For example, you might use a decorator to add logging output on the usage of the contained element without changing any behaviour of the contained element.
    • 新增動作方便,使用者無感。
    • (例如原本可以畫直的捲軸,現在也可以畫橫的捲軸,而使用者可以直接去畫橫捲軸物件出來,不會感覺有什麼問題)

 

POSD學習筆記(5)-Bridge Pattern

目的:
  1. 將抽象類別與實作分離,降低耦合度,使兩者能分別變更(例如更改其中一個實作的method,前端並不會知道你有更改)。
  2. 隱藏實作,不讓使用者知道。
  3. 當抽象類別與實作都有龐大的分支時,減少新增分支所需的工。


Coding學習筆記(1)-泛型與多型

多型:很簡單,實作多個名稱相同但是輸入參數不同的method。
           Ex:public methodName(varA varName)
                   public methodName(varB varName)
                   public methodName(varC varName)
           如上我們做出三個相同名稱但是參數Type不同的method。

泛型:聽說是新的技術,但是我在書裡有看過,以java為例
           Ex: public class className<T>{
                             private T unknownType; //不知道型態的變數
                      
                             //constructor把不知道型態的變數裝進來
                             className(T constructorVar){
                                      unknownType = constructorVar;
                             }

                             //取得不知道型態的變數
                             public T getVar(){
                                      return unknownType;
                             }
                    }
           使用上必須小心,必須要很清楚資料型態,不然會發生容器不相容的問題。

POSD學習筆記(4)-Adapter Pattern

有兩種Adapter Pattern的實現方法:
1. Adapter繼承被轉換的Class
2. Adapter new 被轉換的Class(擁有◆→)

1的優點在於實作容易,且因繼承的關係,容易變更(複寫)被轉換Class的method,但是Adapter被限制只能轉換有繼承的Class(不要想用多重繼承,很難駕馭的架構,但是可以透過新的泛型技術解決,泛型不同於多型)。

2沒有1的問題(利用多型,可以做出適合各種Type的method),但是比較難實作,且因為是外部Class,不是繼承,所以不容易變更(複寫)被轉換Class的method。