About Me

我的相片
Mr. Pigg
Taipei, Taiwan
檢視我的完整簡介

星期四, 3月 06, 2008

Design Pattern - Iterator & Composite Pattern

Iterator Pattern
要點
如果有物件類部包含一聚合的attribute, 需要被瀏覽檢視, 有的物件是用ArrayList or List or Hashtable,
外部使用者必須面對不同的對象處理, 改封裝成Iterator, 可以隱藏內部實際的implementation, 讓使用者面對一致的對象處理

可利用java.util.Iterator


Composite Pattern
要點 讓屬狀結構中的Node與Leaf實踐同一介面, 讓需要瀏覽數的外部使用者可以用一致的方法處理物件
對於互相不支援的method, 可用UnsupportedException 處理


Composite Iterator
用Iterator的方式可以瀏覽一個樹狀結構, 需要紀錄訪問的Node等


設計守則
一個類別應該只據有一個改變的理由

0 意見: