2013年11月12日 星期二

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.
    • 新增動作方便,使用者無感。
    • (例如原本可以畫直的捲軸,現在也可以畫橫的捲軸,而使用者可以直接去畫橫捲軸物件出來,不會感覺有什麼問題)

 

沒有留言:

張貼留言