Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.

Similar presentations


Presentation on theme: "Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03."— Presentation transcript:

1 Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03

2 Outline of Presentation 1. Intent 2. Motivation 3. Applicability 4. Structure 5. Participants 6. Collaborations 7. Consequences 8. Implementation 9. Known Uses 10. Related Patterns Follows outline of notes pages 163-173 of Design Patterns Text.

3 What is a composite?

4 A structural pattern A structural pattern Used to “compose” objects into a tree structure hierarchy Used to “compose” objects into a tree structure hierarchy Used when individual objects and composites of objects are to be treated uniformly Used when individual objects and composites of objects are to be treated uniformly

5 Motivation Composites prevent the client from having to distinguish between primitive and container objects Composites prevent the client from having to distinguish between primitive and container objects Base of the composite structure is an abstract class that can represent both types of objects mentioned above Base of the composite structure is an abstract class that can represent both types of objects mentioned above –Ex.) Graphic Class

6 Applicability When “part-whole” hierarchies of objects are needed in software design When “part-whole” hierarchies of objects are needed in software design When clients desire to ignore the difference between composites and individual objects When clients desire to ignore the difference between composites and individual objects

7 Structure Composite Leaf Composite Leaf

8 Participants Component Component –Declares the interface for objects in the composition –Implements all default behaviors –Declares an interface for manipulating its child components –May define an interface for accessing a parent in a strucure

9 Participants Leaf Leaf –Represents a leaf object in the composition Ex.) Rectangle, Circle, Text… Ex.) Rectangle, Circle, Text… –Leaf means no children stem from this object –Defines primitive objects behaviors

10 Participants Composite Composite –Defines behaviors for components which will have children –Stores these child components –Implements all child related operations in the previously mentioned Component interface –Ex.) Picture

11 Participants Client Client –Uses the Component interface for object manipulation within the composite

12 Collaborations Clients use the Component interface to interact with the Composite structure. Clients use the Component interface to interact with the Composite structure. If leaf is receiver  request is handled directly If leaf is receiver  request is handled directly If composite is receiver  forward request to child components (operations may be performed before or after forwarding. If composite is receiver  forward request to child components (operations may be performed before or after forwarding.

13 Consequences Primitive and composite objects are defined in a class hierarchy. Primitive and composite objects are defined in a class hierarchy. The process of “composing” a primitive object into a composite object occurs over and over recursively. The process of “composing” a primitive object into a composite object occurs over and over recursively. By treating primitive and composite objects uniformly, client is simplified. By treating primitive and composite objects uniformly, client is simplified. Easy to add new components. Easy to add new components. MAY MAKE DESIGN OVERLY GENERAL! MAY MAKE DESIGN OVERLY GENERAL! –Difference in handling composite and primitive obj.

14 Implementation Explicit parent references Explicit parent references Sharing Components Sharing Components Maximizing the Component interface Maximizing the Component interface Declaring the child management operations Declaring the child management operations List of Components? List of Components? Child Ordering Child Ordering Caching Caching Who should delete components Who should delete components Best data Structure? Best data Structure?

15 Explicit Parent Referencing Simplifies traversal while moving up structure Simplifies traversal while moving up structure Supports the “chain of responsibility” pattern Supports the “chain of responsibility” pattern Parents are defined in component class Parents are defined in component class Parents are changed only in the add and remove operations Parents are changed only in the add and remove operations

16 Sharing Components Children can store multiple parents to allow for component sharing. Children can store multiple parents to allow for component sharing.

17 Maximizing Component Interface Defines as many common operations between leaf and composite as possible. Defines as many common operations between leaf and composite as possible. Leaf and composite sub classes can overload default operations from component class Leaf and composite sub classes can overload default operations from component class

18 Declaring Child Management Operations Where to define add or remove? Where to define add or remove? For transparency, define class at root of hierarchy  allows all component classes uniform treatment. For transparency, define class at root of hierarchy  allows all component classes uniform treatment. For safety, define in composite class so all attempts to add/remove are caught during compile/run time. Transparency is lost because now leaf and composite have different interfaces For safety, define in composite class so all attempts to add/remove are caught during compile/run time. Transparency is lost because now leaf and composite have different interfaces

19 List of components For component to implement a list of components, there should be very few children, otherwise the space penalty will be incurred every time a new leaf is created. For component to implement a list of components, there should be very few children, otherwise the space penalty will be incurred every time a new leaf is created.

20 Child ordering Not necessarily an issue, but for some composites like the graphics class, ordering may represent a “front-to-back” structure and necessary operations need to be made to arrange the children properly. Not necessarily an issue, but for some composites like the graphics class, ordering may represent a “front-to-back” structure and necessary operations need to be made to arrange the children properly.

21 Caching Caching can improve performance by caching certain search queries if the structure is going to be traversed multiple times. Caching can improve performance by caching certain search queries if the structure is going to be traversed multiple times.

22 Who should delete components? If the language being used does not support garbage collection, make the composite class responsible, otherwise let the garbage collection classes take care of the problem. If the language being used does not support garbage collection, make the composite class responsible, otherwise let the garbage collection classes take care of the problem.

23 Best data structure? Pick the most efficient structure to store your child lists following the rules as usual for vectors, trees, linked lists, etc. Pick the most efficient structure to store your child lists following the rules as usual for vectors, trees, linked lists, etc.

24 Sample Code class Equipment{ class Equipment{ public: public: virtual ~Equipment(); virtual ~Equipment(); const char* Name() {return _name;} const char* Name() {return _name;} virtual Watt Power(); virtual Watt Power(); virtual Currency NetPrice(); virtual Currency NetPrice(); virtual Cureency Discount Price(); virtual Cureency Discount Price(); virtual void Add(Equipment*); virtual void Add(Equipment*); virtual void Remove(Equipment*); virtual void Remove(Equipment*); virtual Iterator * CreateIterator(); virtual Iterator * CreateIterator(); protected: protected: Equipment(const char*_name); Equipment(const char*_name); private: private: const char* _name; const char* _name; }; };

25 Known Uses Object oriented systems Object oriented systems –Smalltalk Model, View, Controller Primitive assignments that perform an operation on two registers and assign the result to a third. Primitive assignments that perform an operation on two registers and assign the result to a third. An assignment with a source register but no source (assigned before routine starts). An assignment with a source register but no source (assigned before routine starts).

26 Related Patterns Pg. 223, Chain of responsibility Pg. 223, Chain of responsibility Pg. 175, Decorator Pg. 175, Decorator Pg. 195, Flyweight Pg. 195, Flyweight Pg. 257, Iterator Pg. 257, Iterator Pg. 331, Visitor Pg. 331, Visitor

27 Questions

28 Bibliography Design Patterns, Gamma, Helm, Johnson, Vlissides. Addison-Wesley 1995. Pgs. 163-173. Design Patterns, Gamma, Helm, Johnson, Vlissides. Addison-Wesley 1995. Pgs. 163-173.


Download ppt "Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03."

Similar presentations


Ads by Google