The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.

Slides:



Advertisements
Similar presentations
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Design Patterns Design Patterns Composite Pattern Observer Pattern.
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
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)
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
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.
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.
Design Pattern Interpreter By Swathi Polusani. What is an Interpreter? The Interpreter pattern describes how to define a grammar for simple languages,
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,
Strategy Design Patterns CS 590L - Sushil Puradkar.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
GoF: Document Editor Example Rebecca Miller-Webster.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
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.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
Introduction (Continued) Design Patterns (I) Lecture Two.
The Visitor Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
COMPOSITE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
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.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
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
Factory Patterns 1.
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
Composite Pattern Context:
Behavioral Patterns Part-I introduction UNIT-VI
13. Composite Pattern SE2811 Software Component Design
13. Composite Pattern SE2811 Software Component Design
Composite Design Pattern By Aravind Reddy Patlola.
Presentation transcript:

The Composite Pattern.

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

Composite Pattern Motivation Graphic applications let users build complex diagrams out of simple components. A simple implementation can define a class for primitives and other classes that act as containers for these. Problem: The code that uses theses classes is forced to treat primitives and container objects differently.

Composite Pattern Motivation: - Using Composite pattern clients do not need to make this distinction. - Graphic, Line, Text, Picture The key: The abstract class that represent both primitives and their containers.

Composite Pattern

Applicability (use the 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

Composite pattern Structure:

Composite Pattern Composite Object structure

Composite Pattern Participants –Component (Graphic) Declares the 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 –Leaf (Line, Text, Rectangle) Represents the 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 –Client Manipulates objects in the composition through the Component interface

Composite Pattern Collaborations Clients use the Component class interface to interact with all objects in the composite structures. If the recipient is a leaf it is handled directly, If it is a composite, it is passed to its children.

Composite Pattern Consequences 1- Wherever client code expects a primitive, it can also take a composite object 2- Makes the client simple 3- Makes it easy to add new kinds of components 4- Disadvantage: Sometimes you want a composite to have only certain components, you can not rely on type system to enforce those restrictions.

Composite Pattern Implementation –Explicit Parent References The usual place to define the parent reference is in the component class. The easiest way is to add and remove the reference when a leaf is added or removed from the composite –Sharing Components Share components to reduce storage requirements Becomes difficult when? When a component can only have one parent Which pattern makes this easier? Flyweight –Maximizing the Component Interface Component class should define as many common operations for the Composite and Leaf classes as possible How returning children would be implemented for leaf for example? –Declaring the Child Management Operations Trade off between: Transparency vs Uniformity Safety Clients may try to do meaningless things like add and remove objects from leaves It is better let component fail and raise an exception if it is not allowed to add or remove children

Composite Pattern

Implementations (cont.) –Should Component Implement a List of Components? –Child Ordering Sometimes it is useful to provide ordering. To do this just be careful when writing your Add/Remove children methods Iterator pattern (p257) comes in handy here –Caching to Improve Performance If caching is needed extra caching information should be stored in the Composite class –Who Should Delete Components? In a language without Garbage Collection its best to have Composite responsible for deleting its children when it’s destroyed –What’s the Best Data Structure for Storing Components? Basically any will work (Linked List, trees, arrays, Hash Tables, etc) The trick to choosing will be (as per usual) the efficiency trade-offs Sometimes composites have a variable for each child, although this requires each subclass of Composite to implement its own management interface. See Interpreter (p243) for an example.

Composite Pattern Known Uses Almost all complex systems, MVC, every user interface toolkit or framework Related Patterns Decorator is often used with composite, Flyweight lets you share components, but they can not refer to their parents Iterator can be used to traverse the composites Visitor localizes operations and behaviors that otherwise be distributed across composite and leaf classes.

Example1 Situation: A GUI system has window objects which can contain various GUI components (widgets) such as, buttons and text areas. A window can also contain widget container objects which can hold other widgets. Solution 1: What if we designed all the widgets with different interfaces for "updating" the screen? We would then have to write a Window update() method as follows:

Example1 public class Window { Button[] buttons; Menu[] menus; TextArea[] textAreas; WidgetContainer[] containers; public void update() { if (buttons != null) for (int k = 0; k < buttons.length; k++) buttons[k].draw(); if (menus != null) for (int k = 0; k < menus.length; k++) menus[k].refresh(); // Other widgets handled similarly. if (containers != null) for (int k = 0; k < containers.length; k++ ) containers[k].updateWidgets(); }... }

Example 1 Well, that looks particularly bad. If we want to add a new kind of widget, we have to modify the update() method of Window to handle it. Solution 2: We should always try to program to an interface, right? So, let's make all widgets support the Widget interface, either by being subclasses of a Widget class or implementing a Java Widget interface. Now our update() method becomes:

Example1

Example2

Example3- The Java AWT