Composite Design Pattern. Motivation – Dynamic Structure.

Slides:



Advertisements
Similar presentations
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Composite Design Pattern (1) –A structural design pattern.
March Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.
Fall 2009ACS-3913 Ron McFadyen Composite Pattern Problem: How do we treat a composition structure of objects the same way as a non-composite object? Arises.
The Composite Pattern. Owners (No Twins, Expos) Managers (No Twins, Expos) Congress (No Twins, Expos) Media (No Twins, Expos) Radio (No Twins, Expos)
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Chapter 8, Object Design Introduction to Design Patterns
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
3-1 Composite Design Pattern Rick Mercer. 2 Composite Pattern Context : –Often complex structures are built with container and primitive objects. Container.
BTS530: Major Project Planning and Design The Composite Pattern All References and Material From: Design Patterns,by (GOF!) E.Gamma, R.Helm, R.Johnson,
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Structural Design Patterns
Object Oriented Design David Talby. Welcome! n Introduction n UML u Use Case Diagrams u Interaction Diagrams u Class Diagrams n Design Patterns u Composite.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
Composite Pattern ( ) Pattern Hatching Chpt 1-2 Presentation by Joe Barzilai 1/30/2006.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
COMPOSITE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Pick of the day 13-Jan-2003 Lecture 3 Common Patterns in Static Design.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Computer Science 313 – Advanced Programming Topics.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Composite Pattern Himanshu Gupta Shashank Hegde CSE776 – Design Patterns Fall 2011 Good composition is like a suspension bridge - each line adds strength.
Composite The Recursive Tree Structure in Design Pattern terms…
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Abstract Factory Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Slide design: Dr. Mark L. Hornick
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Factory Patterns 1.
Composite Design Pattern
Composite Pattern SE2811 Software Component Design
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Abstract Factory Pattern
Iterator and Composite Design Patterns
Intent (Thanks to Jim Fawcett for the slides)
More Design Patterns 1.
Design Patterns - A few examples
More Design Patterns 1.
Design Patterns A Case Study: Designing a Document Editor
Jim Fawcett CSE776 – Design Patterns Summer 2003
Software Development CSE3311
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Lesson 5: More on Creational Patterns
13. Composite Pattern SE2811 Software Component Design
Software Design Lecture : 35.
13. Composite Pattern SE2811 Software Component Design
Object Oriented Design
Composite Design Pattern By Aravind Reddy Patlola.
Software Design Lecture : 36.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

Composite Design Pattern

Motivation – Dynamic Structure

Motivation – Static Structure

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.

Structure

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

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.

Structure

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

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.

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.

Tree Structure

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.

14 Directory / File Example – Object Structure / bin/user/tmp/ file1 file2 subdir/file3 file4 file5 Composite Leaf Directory = Composite File = Leaf

15 Directory / File Example – Classes One class for Files (Leaf nodes) One class for Directories (Composite nodes) – Collection of Directories and Files How do we make sure that Leaf nodes and Composite nodes can be handled uniformly? – Derive them from the same abstract base class Leaf Class: FileComposite Class: Directory

16 Directory / File Example – Structure Abstract Base Class: Node Leaf Class: FileComposite Class: Directory

17 Directory / File Example – Operation Abstract Base Class: Node size() in bytes of entire directory and sub- directories Leaf Class: File size () of file Composite Class: Directory size () Sum of file sizes in this directory and its sub-directories long Directory::size () { long total = 0; Node* child; for (int i = 0; child = getChild(); ++i; { total += child->size(); } return total; }

18 Inventory Example – Problems to Consider Abstract Base Class: Inventory Item Leaf Class: Object Composite Class: Container Backpack Cloth BagJeweled Dagger RubyRingWine Bottle Do you really want all leaf Operations to be defined in Component class? Is Wine Bottle a Composite or does it have an operation for Fill(Liquid)? If Ruby is pried from Dagger, how will this be handled? Would you allow user to put other items in the wine bottle? How do you handle the relative size of items to be placed in a backpack?

19 Consequences Solves problem of how to code recursive hierarchical part-whole relationships. Client code is simplified. – Client code can treat primitive objects and composite objects uniformly. – Existing client code does not need changes if a new leaf or composite class is added (because client code deals with the abstract base class). Can make design overly general. – Can ’ t rely on type system to restrict the components of a composite. Need to use run-time checks.