Winter 2015ACS - 3913 Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent.

Slides:



Advertisements
Similar presentations
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Advertisements

Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Winter 2007ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Winter 2007ACS-3913 Ron McFadyen1 Also known as publish/subscribe The essence of this pattern is that one or more objects (called observers or listeners)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Composite Design Pattern (1) –A structural design pattern.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
March Ron McFadyen1 Composite Used to compose objects into tree structures to represent part-whole hierarchies Composite lets clients treat.
Trees Chapter 8.
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.
Fall 2009ACS-3913 Ron McFadyen1 Decorator Pattern The Decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
COMP5712 Tutorial 4. 2 Using an Array to Represent a Heap When a binary tree is complete – Can use level-order traversal to store data in consecutive.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Binary Trees Terminology A graph G = is a collection of nodes and edges. An edge (v 1,v 2 ) is a pair of vertices that are directly connected. A path,
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Sept 2004Ron McFadyen Decorator Pattern The decorator pattern allows us to enclose an object inside another object. The enclosing object is called.
Fall 2007ACS Ron McFadyen1 Composite Pattern (see pages ) A composite is a group of objects in which some objects contain others; one object.
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
Feb Ron McFadyen1 Iterator Pattern Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate interface.
Trees Chapter 25 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
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.
Trees Chapter 23 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Winter 2011ACS-3913 Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Chapter 18 - basic definitions - binary trees - tree traversals Intro. to Trees 1CSCI 3333 Data Structures.
Composite Design Pattern. Motivation – Dynamic Structure.
More on Trees CS1316: Representing Structure and Behavior.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Trees. Tree Terminology Chapter 8: Trees 2 A tree consists of a collection of elements or nodes, with each node linked to its successors The node at the.
Trees & Graphs Chapter 25 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
CS Data Structures Chapter 15 Trees Mehmet H Gunes
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Chapter 19 Implementing Trees and Priority Queues Fundamentals of Java.
Chapter 9: The Iterator Pattern
程式語言結構 Final Project. Goal The student can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
Jan Ron McFadyen1 Decorator Sometimes we need a way to add responsibilities to an object dynamically and transparently. The Decorator pattern.
程式語言結構 Final Project 老師: Gwan-Hwan Hwang. Goal The students can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
Topic 15 The Binary Search Tree ADT Binary Search Tree A binary search tree (BST) is a binary tree with an ordering property of its elements, such.
Computer Science 112 Fundamentals of Programming II Introduction to Trees.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100I: Data.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
CS 210 Iterator Pattern October 31 st, Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another.
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.
Iterator Pattern. Traversing two different collections  When bringing two previously developed objects together, it can be difficult to change an implementation.
Trees Ellen Walker CPSC 201 Data Structures Hiram College.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Collaboration diagrams. Deployment diagrams. Lesson 4.
Chapter 10 Trees © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
1 Iterator Pattern (A Behavioral Pattern) Prepared by: Neha Tomar.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture13.
(c) University of Washington20-1 CSC 143 Java Trees.
Fundamentals of Programming II Introduction to Trees
Tree.
Section 8.1 Trees.
Iterator and Composite Design Patterns
Composite Pattern Oct 7, 2003 A composite is a group of objects in which some objects contain others; one object may represent groups, and another.
Jim Fawcett CSE776 – Design Patterns Summer 2003
Menu item at a restaurant
Chapter 20: Binary Trees.
Recursive Relationship
Presentation transcript:

Winter 2015ACS Ron McFadyen1 Composite Pattern A composite is a group of objects in which some objects contain others; one object may represent collections, and another may represent an individual item, a leaf. The composite class diagram enables a tree structure – a hierarchy. The operations are appropriate for processing and traversing trees.

Winter 2015ACS Ron McFadyen2 Composite Pattern Component operation() Leaf operation() Composite operation() other() * Client Note that Composite has references to possibly many Components. Each node of the Component structure can respond to some common operation(s). A client can send the common operation to Component and the structure responds “appropriately”. The client sees Component … the client does not see the complexity of Component.

Winter 2015ACS Ron McFadyen3 Composite Pattern First example Class diagram One operation, display. How is display handled by the various classes? How are associations implemented? What hierarchy is instantiated?

Winter 2015ACS Ron McFadyen4 Composite Pattern Consider the Head First example What is the class diagram? Who is the client? What operations are defined for the component, the composite, and the leaf? How are they carried out? How are the associations implemented in the code? What is the hierarchy that is instantiated?

Winter 2015ACS Ron McFadyen5 Composite Pattern – Text example MenuComponent add() remove() getChild() print() … MenuItem getName() getDescription() getPrice() isVegetarian() print() Menu add() remove() getChild() getName() getDescription() print() * Waitress name description vegetarian price name description printMenu()

Winter 2015ACS Ron McFadyen6 Composite Pattern – Text example Menu menuComponents: ArrayList add() remove() getChild() print()… Note the text implements the association from Menu to MenuComponents with an array list data type. The association from Waitress to MenuComponent …

Winter 2015ACS Ron McFadyen7 Composite Pattern – Text example A waitress can ask a menu component to print itself… print() is a common operation. What happens when a waitress sends print() to a menu component? (Given a specific hierarchical menu, what messages are sent?)

Winter 2015ACS Ron McFadyen8 Composite Pattern – Text example In the Menu class, print() is appropriate for an internal node … it sends print() to each of its children public void print() { System.out.print("\n" + getName()); System.out.println(", " + getDescription()); System.out.println(" "); Iterator iterator = menuComponents.iterator(); while (iterator.hasNext()) { MenuComponent menuComponent = (MenuComponent)iterator.next(); menuComponent.print(); }

Winter 2015ACS Ron McFadyen9 Composite Pattern – Text example In the MenuItem class, print() is appropriate for a leaf: public void print() { System.out.print(" " + getName()); if (isVegetarian()) { System.out.print("(v)"); } System.out.println(", " + getPrice()); System.out.println(" -- " + getDescription()); }

Winter 2015ACS Ron McFadyen10 Composite Pattern A BCD RSTV WZ Tree traversals: Pre-order traversal Visit a node and then its children: ABCRSWZTVD Post-order traversal Visit the children and then the parent: BRWZSTVCDA Level-order traversal Visit nodes on one level and then the next: ABCDRSTVWZ Which traversal is utilized in the Head First example?