13. Composite Pattern SE2811 Software Component Design

Slides:



Advertisements
Similar presentations
4. Object-Oriented Programming Procedural programming Structs and objects Object-oriented programming Concepts and terminology Related keywords.
Advertisements

Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
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.
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)
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
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.
Composite Design Pattern. Motivation – Dynamic Structure.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
BTS530: Major Project Planning and Design The Composite Pattern All References and Material From: Design Patterns,by (GOF!) E.Gamma, R.Helm, R.Johnson,
The Adapter Pattern SE-2811 Dr. Mark L. Hornick 1.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
The Façade Pattern SE-2811 Dr. Mark L. Hornick 1.
Object Oriented Software Development
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.
UML Class Diagram notation Indicating relationships between classes SE-2030 Dr. Mark L. Hornick 1.
COMPOSITE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Week 6, Day 3 The Gang of Four and more … A new design pattern SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Model-View-Controller A Design Pattern SE-2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Composite Pattern Himanshu Gupta Shashank Hegde CSE776 – Design Patterns Fall 2011 Good composition is like a suspension bridge - each line adds strength.
UML Diagrams: Class Diagrams The Static Analysis Model
Slide design: Dr. Mark L. Hornick
UofC Large Display Framework
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Week 2, Day 1: The Factory Method Pattern
Instructor: Dr. Hany H. Ammar
Composite Design Pattern
Composite Pattern SE2811 Software Component Design
Iterator and Composite Design Patterns
Intent (Thanks to Jim Fawcett for the slides)
SE-2811 Software Component Design
More Design Patterns 1.
Model-View-Controller
Design Patterns - A few examples
More Design Patterns 1.
Design Patterns A Case Study: Designing a Document Editor
Domain Class Diagram Chapter 4 Part 2 pp
Jim Fawcett CSE776 – Design Patterns Summer 2003
Software Development CSE3311
Composite Pattern Context:
SE-2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
Introduction to Data Structure
14. Factory Pattern SE2811 Software Component Design
SE2811 Software Component Design Dr. Rob Hasker
10. Façade Pattern SE2811 Software Component Design
14. Factory Pattern SE2811 Software Component Design
12. Command Pattern SE2811 Software Component Design
Software Design Lecture : 35.
13. Composite Pattern SE2811 Software Component Design
Composite Design Pattern By Aravind Reddy Patlola.
16. Visitors SE2811 Software Component Design
Dr. Chih-Kuo Yeh 葉智國 Computer Graphics Dr. Chih-Kuo Yeh 葉智國
16. Visitors SE2811 Software Component Design
Software Design Lecture : 36.
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

13. Composite Pattern SE2811 Software Component Design Dr. Rob Hasker (based on slides by Dr. Mark Hornick) 13. Composite Pattern 1

Composite Pattern context Graphics drawing Render graphic primitives (e.g. lines, rectangles, ellipses,…) Also subdrawings: groups of primitives translated, rotated, or scaled as a unit User-Interface Menus A menu: multiple menu items. Each menu item can in turn be a menu (sub-menu). Generally, any application implementing a hierarchical structure A object can contain many sub-objects Each sub-object can in turn contain an object. Q: Do any JavaFX classes implement a similar hierarchy??? 2

The Composite Pattern is applied in situations involving object heirarchies The problem A collection of objects forms a hierarchy Each object may be An individual (primitive, leaf, or part) object A composition of other objects (composite) We want to treat all objects uniformly No special treatment (if, instanceof) for composite objects (sub-drawings or sub-menus) Solution Compose objects into recursive tree structures via the Composite Pattern 3

Composite Pattern: compose objects into tree structures to represent part-whole hierarchies aComposite aPart aPart aPart aComposite This pattern allows clients to treat individual objects (Parts) and compositions of objects (Composites) uniformly. aPart aPart aPart

Example You want to build a new computer. Let’s configure the computer as a system of hierarchical components. composite computer keyboard System Unit monitor Fan HDD Cabinet part Chassis CPU Memory GPU Motherboard 5

Composite Pattern class diagram Client app Component defines an interface (or abstract class) for all objects: both Leaf and Composite There may be variations in the names of the add(), remove(), and getChildren() methods ClientApp uses the Component Interface to manipulate objects in the composition by calling add(), remove(), and context-specific operations. A part/leaf can have no children; methods like add(), remove() don’t make sense for this class, but are still inherited from Component . Composite defines the behavior of those Components having children and stores the child Components.

One approach: include composite behavior in components public interface Component { // behaviors for Part and Composite public void add(Component c); public void remove(Component c); public List<Component> getChildren(); public abstract double getPrice(); // Part-specific ... <other Part behaviors> } rather than an Abstract Class?

public class Composite implements Component { private String name; private double price; private List<Component> components; Composite (String name, double basePrice) { // ctor details components = new ArrayList<Component>(); } // essential Composite behaviors: public void add(Component c) { components.add(c); public void remove (Component c){ components.remove(c); } // continued next slide

...public class Composite implements Component { // ...continued from previous slide // context-specific behavior public double getPrice() { double compositePrice = price; for(Component c: components) { CompositePrice += c.getPrice(); } return compositePrice;

public class Part implements Component { // Part is a “Leaf” private String name; private double price; Part(String name, double price) { this.name = name; this.price = price; } // Composite-related behaviors public void add(Component c) { // what should we do here?? // do nothing? Throw exception? Return a true/false? public void remove(Component c){ } // alternatives: // Throw an exception? // Return a null? }

Consequences Defines class hierarchy Simplifies client Leafs(Parts), Composites Composite may replace Leaf/Part in any client operation Simplifies client No special treatment for Composites vs Parts – every object can simply be treated as a Component Clients don’t know (don’t care) whether they are dealing with Leaf/Part or Composite. The specific type of object is transparent Easy to add new Component types Just add new derived class No client changes needed 11

Consequences Concern: Composite Pattern represents a classic tradeoff Two responsibilities in one class A Component defines both Part and Composite behavior Also, a Part cannot logically support certain behaviors (add, remove, getChildren) …or can we just look at a Part as a Composite with no children??? At any rate, type safety is compromised Composite Pattern represents a classic tradeoff Trading transparency for type safety This violates LSP 12