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

28
Software Design and Software Design and Documentation Documentation Individual Presentation: Individual Presentation: Composite Pattern Composite Pattern 9/11/03 9/11/03
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    218
  • download

    1

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

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

Software Design and Software Design and DocumentationDocumentation

Individual Presentation:Individual Presentation:Composite PatternComposite Pattern

9/11/039/11/03

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

Outline of PresentationOutline of Presentation

1.1. IntentIntent

2.2. MotivationMotivation

3.3. ApplicabilityApplicability

4.4. StructureStructure

5.5. ParticipantsParticipants

6.6. CollaborationsCollaborations

7.7. ConsequencesConsequences

8.8. ImplementationImplementation

9.9. Known UsesKnown Uses

10.10.Related Related PatternsPatterns

Follows outline of notes pages 163-173 of Design Patterns Text.

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

What is a composite?What is a composite?

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

What is a composite?What is a composite?

• A structural patternA structural pattern

• Used to “compose” objects into a Used to “compose” objects into a tree structure hierarchytree structure hierarchy

• Used when individual objects and Used when individual objects and composites of objects are to be composites of objects are to be treated uniformlytreated uniformly

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

MotivationMotivation

• Composites prevent the client from Composites prevent the client from having to distinguish between having to distinguish between primitive and container objectsprimitive and container objects

• Base of the composite structure is an Base of the composite structure is an abstract class that can represent abstract class that can represent both types of objects mentioned both types of objects mentioned aboveabove– Ex.) Graphic ClassEx.) Graphic Class

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

ApplicabilityApplicability

• When “part-whole” hierarchies of When “part-whole” hierarchies of objects are needed in software objects are needed in software designdesign

• When clients desire to ignore the When clients desire to ignore the difference between composites and difference between composites and individual objectsindividual objects

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

StructureStructure

Composite

Leaf Leaf Composite Leaf

Leaf Leaf Leaf

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

ParticipantsParticipants

• ComponentComponent– Declares the interface for objects in the Declares the interface for objects in the

compositioncomposition– Implements all default behaviorsImplements all default behaviors– Declares an interface for manipulating Declares an interface for manipulating

its child componentsits child components– May define an interface for accessing a May define an interface for accessing a

parent in a strucureparent in a strucure

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

ParticipantsParticipants

• LeafLeaf– Represents a leaf object in the Represents a leaf object in the

compositioncomposition•Ex.) Rectangle, Circle, Text…Ex.) Rectangle, Circle, Text…

– Leaf means no children stem from this Leaf means no children stem from this objectobject

– Defines primitive objects behaviorsDefines primitive objects behaviors

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

ParticipantsParticipants

• CompositeComposite– Defines behaviors for components which Defines behaviors for components which

will have childrenwill have children– Stores these child componentsStores these child components– Implements all child related operations Implements all child related operations

in the previously mentioned Component in the previously mentioned Component interfaceinterface

– Ex.) PictureEx.) Picture

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

ParticipantsParticipants

• ClientClient– Uses the Component interface for object Uses the Component interface for object

manipulation within the compositemanipulation within the composite

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

CollaborationsCollaborations

• Clients use the Clients use the ComponentComponent interface to interface to interact with the interact with the Composite Composite structure.structure.

• If leaf is receiver If leaf is receiver request is handled request is handled directlydirectly

• If composite is receiver If composite is receiver forward forward request to child components (operations request to child components (operations may be performed before or after may be performed before or after forwarding.forwarding.

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

ConsequencesConsequences

• Primitive and composite objects are Primitive and composite objects are defined in a class hierarchy.defined in a class hierarchy.

• The process of “composing” a primitive The process of “composing” a primitive object into a composite object occurs over object into a composite object occurs over and over recursively.and over recursively.

• By treating primitive and composite By treating primitive and composite objects uniformly, client is simplified.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 Difference in handling composite and primitive obj.obj.

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

ImplementationImplementation

• Explicit parent Explicit parent referencesreferences

• Sharing ComponentsSharing Components

• Maximizing the Maximizing the Component Component interfaceinterface

• Declaring the child Declaring the child management management operationsoperations

• List of List of Components?Components?

• Child OrderingChild Ordering

• CachingCaching

• Who should delete Who should delete componentscomponents

• Best data Best data Structure?Structure?

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

Explicit Parent ReferencingExplicit Parent Referencing

• Simplifies traversal while moving up Simplifies traversal while moving up structurestructure

• Supports the “chain of responsibility” Supports the “chain of responsibility” patternpattern

• Parents are defined in component Parents are defined in component classclass

• Parents are changed only in the add Parents are changed only in the add and remove operationsand remove operations

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

Sharing ComponentsSharing Components

• Children can store multiple parents Children can store multiple parents to allow for component sharing.to allow for component sharing.

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

Maximizing Component Maximizing Component InterfaceInterface

• Defines as many common operations Defines as many common operations between leaf and composite as between leaf and composite as possible.possible.

• Leaf and composite sub classes can Leaf and composite sub classes can overload default operations from overload default operations from component classcomponent class

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

Declaring Child Management Declaring Child Management OperationsOperations

• Where to define add or remove?Where to define add or remove?

• For transparency, define class at root of For transparency, define class at root of hierarchy hierarchy allows all component classes allows all component classes uniform treatment.uniform treatment.

• For safety, define in composite class so all For safety, define in composite class so all attempts to add/remove are caught during attempts to add/remove are caught during compile/run time. Transparency is lost compile/run time. Transparency is lost because now leaf and composite have because now leaf and composite have different interfacesdifferent interfaces

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

List of componentsList of components

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

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

Child orderingChild ordering

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

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

CachingCaching

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

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

Who should delete Who should delete components?components?

• If the language being used does not If the language being used does not support garbage collection, make the support garbage collection, make the composite class responsible, composite class responsible, otherwise let the garbage collection otherwise let the garbage collection classes take care of the problem.classes take care of the problem.

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

Best data structure?Best data structure?

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

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

Sample CodeSample 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<Equipment*>* CreateIterator();virtual Iterator<Equipment*>* CreateIterator();• protected:protected:• Equipment(const char*_name);Equipment(const char*_name);• private:private:• const char* _name;const char* _name;• };};

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

Known UsesKnown Uses

• Object oriented systemsObject oriented systems– Smalltalk Model, View, ControllerSmalltalk Model, View, Controller

• Primitive assignments that perform Primitive assignments that perform an operation on two registers and an operation on two registers and assign the result to a third.assign the result to a third.

• An assignment with a source register An assignment with a source register but no source (assigned before but no source (assigned before routine starts).routine starts).

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

Related PatternsRelated Patterns

• Pg. 223, Chain of responsibilityPg. 223, Chain of responsibility

• Pg. 175, DecoratorPg. 175, Decorator

• Pg. 195, FlyweightPg. 195, Flyweight

• Pg. 257, IteratorPg. 257, Iterator

• Pg. 331, VisitorPg. 331, Visitor

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

QuestionsQuestions

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

BibliographyBibliography

• Design PatternsDesign Patterns, Gamma, Helm, , Gamma, Helm, Johnson, Vlissides. Addison-Wesley Johnson, Vlissides. Addison-Wesley 1995. Pgs. 163-173.1995. Pgs. 163-173.