SlideServe
Menu
Préférences
© SlideServe · v2026

Composite Pattern

egillispie avatar
16 diapositives · 218 Views · Jan 05, 2020

Learn how to compose objects into tree structures to represent part-whole hierarchies using the Composite pattern. Discover its motivation, applicability, structure, participants, collaborators, consequences, known uses, and related patterns.

Composite Pattern - slide 1
01 / 16Composite Pattern
1 / 17

Télécharger la présentation

Composite Pattern
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Composite Pattern Jim Fawcett CSE776 – Design Patterns Summer 2003

  2. Intent • Compose objects into tree structures to represent part-whole hierarchies. • Composite lets clients treat individual objects and compositions of objects in a uniform way.

  3. Motivation – Dynamic Structure

  4. Motivation – Static Structure

  5. Applicability • Use the Composite pattern when: • You need to represent part-whole hierarchies of objects • You want clients to ignore the differences between parts and wholes (individual objects and compositions of objects) • The compositions are created dynamically – at run time – so use composite: • when you need to build a complex system from primitive components and previously defined subsystems. • This is especially important when the construction process will reuse subsystems defined earlier.

  6. Structure – Plan A

  7. Participants – Plan A • Component (Graphic) • declares interface for objects in the composition • implements default behavior for the interface common to all classes, as appropriate • declares an interface for accessing and managing its child components • (optional) defines an interface for accessing a component’s parent in the recursive structure, and implements it if that’s appropriate • Client • manipulates objects in the composition through the Component interface

  8. Participants – Plan A • Leaf • Represents leaf objects in the composition. A leaf has no children. • Defines behavior for primitive objects in the composition. • Composite (picture) • Defines behavior for components having children. • Stores child components. • Implements child-related operations in the Component interface.

  9. Structure – Plan B

  10. Participants – Plan B • Component (Graphic) • declares interface for objects in the composition • Client • manipulates objects in the composition through the Composite interface

  11. Participants – Plan B • Leaf • Represents leaf objects in the composition. A leaf has no children. • Defines behavior for primitive objects in the composition. • Composite (picture) • Defines behavior for components having children. • declares an interface for accessing and managing its child components • Stores child components. • Implements child-related operations in the Component interface.

  12. Collaborators • Plan A:Clients use the Component class interface to interact with objects in the composite structure. • Plan B:Clients use the Composite class interface to interact with objects in the composite structure. • If the recipient is a Leaf then the request is handled directly. • If the recipient is a composite, then it usually forwards request to its child components.

  13. Tree Structure

  14. Consequences • The Composite pattern: • Defines class hierarchies consisting of primitives and composites • When a client expects a primitive it can also take a composite. • Makes it easier to add new components. • Newly defined primitives or composites will work automatically with existing structures and client code. • Can make your design too general. • Since its easy to add new components it is hard to restrict the components of a composite. • Composite doesn’t support using the type system to support these constraints for you, since all components have the same base type.

  15. Know Uses • Authors cite many windowing toolkits. • The FORTH language defines its statements using an extensible dictionary which implements the Composite pattern.

  16. Related Patterns • Decorator – very similar structure and intent • Iterator can be used to traverse Composites.