Implementing the composite pattern lets clients treat individual objects and compositions uniformly 組合模式 : 將物件組合成樹形結再以表示「部份 - 整體」的層次結構 組合模式使得用戶對單個物件和組合物件的使用具有一致性。
Composite can be used when clients should ignore the difference between compositions of objects and individual objects.
Design pattern composite 20120413 joncash 01
1Design Pattern –CompositePresented by Joncash4/13/2012
2Outline• Definition• Motivation• When to Use• Structure• Application• Example
3Definition• The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object.• The intent of a composite is to "compose" objects into tree structures to represent part-whole hierarchies.
4Motivation• When dealing with tree-structured data, programmers often have to discriminate between a leaf-node and a branch. This makes code more complex, and therefore, error prone.
5When to Use• If programmers find that they are using multiple objects in the same way, and often have nearly identical code to handle each of them, then composite is a good choice