1 / 16

Composite Pattern

Composite Pattern. Jim Fawcett CSE776 – Design Patterns Summer 2003. 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. Motivation – Dynamic Structure.

egillispie
Download Presentation

Composite Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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.

More Related