Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Object Oriented Programming Lect. Dr. Daniel POP Universitatea de Vest din Timişoara Facultatea de Matematică şi Informatică.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Design Patterns Design Patterns Composite Pattern Observer Pattern.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
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.
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
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.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
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.
Using Design Patterns to Elaborate upon Design Models Moving from Analysis to Design Examine Design models and initial code to: Improve cohesion Reduce.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
OOMPA Lecture 9 Design Patterns Composite Pattern Observer Pattern.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
1 PH Chapter 1 (pp. 1-10) GoF Composite Pattern (pp ) PH Ch 2 through Fundamentals (pp ) Presentation by Julie Betlach 5/28/2009.
Composite Design Pattern. Motivation – Dynamic Structure.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
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.
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
© Spiros Mancoridis 27/09/ Software Design Topics in Object-Oriented Design Patterns Material drawn from [Gamma95] and [Coplien95] Revised and augmented.
BTS530: Major Project Planning and Design The Composite Pattern All References and Material From: Design Patterns,by (GOF!) E.Gamma, R.Helm, R.Johnson,
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Command Pattern.
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.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Design Patterns Introduction
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
ISBN Object-Oriented Programming Chapter Chapter
Introduction (Continued) Design Patterns (I) Lecture Two.
COMPOSITE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
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.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Composite Pattern Himanshu Gupta Shashank Hegde CSE776 – Design Patterns Fall 2011 Good composition is like a suspension bridge - each line adds strength.
Slide design: Dr. Mark L. Hornick
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Composite Design Pattern
Composite Pattern SE2811 Software Component Design
Iterator and Composite Design Patterns
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
13. Composite Pattern SE2811 Software Component Design
Software Design Lecture : 35.
13. Composite Pattern SE2811 Software Component Design
Composite Design Pattern By Aravind Reddy Patlola.
Software Design Lecture : 36.
Visitor Pattern Intent
Presentation transcript:

Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern

Intent Compose objects into tree structures to represent part-whole hierarchies. Let clients treat individual objects and compositions of objects uniformly.

Motivation Some system let user build complex diagrams out of simple components. A simple implementation could define classes for each concept of the components.  a problem: Code that uses these classes must treat primitive and container objects differently.

Motivation(2) The key of composite pattern is an abstract class that represents both primitives and their containers. A typical composite structures: aPicture aLine aRect aTextaLineaRect

Applicability Use this pattern when  You want to represent part-whole hierarchies of objects.  You want clients to be able to ignore the difference between compositions of objects and individual objects. Client will treat all objects in the composite structure uniformly

Structure Page 164 Component Operation() Add(Component) Remove(Component) GetChild(int) Leaf Operations() Composite Operation() Add(Component) Remove(Component) GetChild(int) children Client

Participants Component  declare the interface for objects in the composition.  implements default behavior.  declares an interface for accessing and managing its child components.  (opt.) defines an interface for accessing a component ’ s parent in the recursive structure, …

Participants(2) Leaf  represents leaf objects in the composition.  define behavior for primitive objects. Composite  defines behavior for components having children.  stores child components.  implements child-related operations in the component interface. Client  manipulates objects in the composition through the Component interface.

Collaborations Client use the Component class interface to interact with objects in the composite structure.  If the recipient is a Leaf, then the request is handled directly.  If not, the it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.

Consequences defines class hierarchies consisting of primitive objects and composite objects. makes the client simple. makes it easier to add new kinds of components. (disadvantage) can make your design overly general.

Implementation issues(1) Explicit parent references: the reference from child to components to their parent.  Simplify the traversal and management of a composite structure: moving up, delete.  The references is usually defined in the component class.  You must maintain the invariant.  Relative pattern: Chain of Responsibility

Implementation issues(2) Sharing components  It ’ s useful to share components to reduce storage requirements.  Difficult if a component can have no more than one parent.  Relative pattern: Flyweight

Implementation issues(3) Maximizing the Component interface  The Component class should define as many common operations for Composite and Leaf classes as possible.  But sometime it conflicts with the principle of class hierarchy design that says a class should only define operations that are meaning ful to its subclasses.  A little creativity: treat the Leaf class as a special kind of Composite class.

Implementation issues(4.1) Declaring the child management operations  Declare it in Component or in Composite? The decision involves a trade-off between safety and transparency.  Define the operations in Component gives you transparency, but cost safty.  Define the operations in Composite gives you safty, but you lose transparency.

Implementation issues(4.2) We emphasize transparency over safety in this pattern. An approach for safty without a type-unsafe cast: declare an operation Composite *GetComposite() in the class Component.  The default operation return null.  Each composite class override it and return this.  You can query wether a component instance is a composite object using GetComposite()

Implementation issues(4.3) class Composite class Component { public: virtual Composite * GetComposite(){return null} } class Composite: public Component { public: virtual Composite * GetComposite(){return this} } if(test = aComponent->GetComposite()) { test->Add(new Leaf); }

Implementation issues(5) Should Component implement a list of Components?  May define the set of children as an instance variable in the Component class where the child access and management operations are declared.  But incurs a space penalty for every leaf.

Implementation issues(6) Child ordering  Many designs specify an ordering on the children of Composite.  When child ordering is an issue, you must design child access and management interfaces carefully.  Related pattern: Iterator.

Implementation issues(7) Caching to improve performance  The Composite class can cache traversal or search information about its children.  The information about its sub-tree can be cached in the parent object. To avoid traverse the sub- tree again and again.  A component will require invalidating the caches of its parents. So you need an interface for telling composites that their caches are invalid.

Implementation issues(8) Who should delete components?  In language without garbage collection, it ’ s best to make a Composite responsible for deleting its children when it ’ s destroyed.  An exception: when Leaf objects are immutable.

Implementation issues(9) What ’ s the best data structure for storing components?  a variety of data structure can be used: liked lists, trees, arrays, hash tables.  The choice depends on efficiency.  It isn ’ t necessary to use a general-purpose data structure: a subclass of Composite can implement its own management interface.  Related pattern: Intrepreter.

Sample Code Equipment: computer and stereo components. class Equipment { virtual ~Equipment(); const char* Name() { return _name;} virtual Watt Power(); virtual Currency NetPrice(); virtual Currency DiscountPrice(); virtual void Add(Equipment *); virtual void Remove(Equipment *); virtual Iterator * CreaterIterator(); Protected: Equipment(const char *); private: const char* _name; }

Sample Code Leaf class: class FloppyDisk : public Equipment{ public: FloppyDisk(const char*); virtual ~FloppyDisk(); virtual Watt Power(); virtual Currency NetPrice(); Virtual Currency DiscountPrice(); }

Sample Code The base class for equip. containing other equip. class CompositeEquipment: public Equipment{ public: virtual ~CompositeEquipment(); virtual Watt Power(); virtual Currency NetPrice(); virtual Currency DiscountPrice(); virtual void Add(Equipment *); virtual void Remove(Equipment *); virtual Iterator *CreatIterator(); protected: CompositeEquipment(const char*); private: List _equipment; }

Sample Code Default Impl. of NetPrice Currency CompositeEquipment::NetPrice() { Iterator *i=CreateIterator(); Currency total = 0; for (i->First(); !i->IsDone(); i->Next() { total += i->CurrentItem()->NetPrice(); }

Sample Code class for computer chassis class Chassis: public CompositeEquipment{ public: Chassis(const char*); virtual ~Chassis(); virtual Watt Power(); virtual Currency NetPrice(); virtual Currency DiscountPrice(); };

Sample Code Use the classes Cabinet * cabinet = new Cabinet( “ PC Cabinet ” ); Chassis* chassis = new Chassis( “ PC Chassis ” ); Cabinet->Add(chassis); Bus * bus = new Bus( “ NCA Bus ” ); bus->Add(new Card( “ 16Mbs Token Ring ” )); Chassis->Add(bus); Chassis->Add(new FloppyDisk( “ 3.5in Floppy ” )); cout NetPrice() <<endl.