Tecniche di Progettazione: Design Patterns

Similar documents
Tecniche di Progettazione: Design Patterns

Tecniche di Progettazione: Design Patterns

09. ITERATOR AND COMPOSITE PATTERNS Well-Managed Collections. There are lots of ways to stuff objects into a collection.

Template Method, Iterator & Composite

Design Pattern: Composite

The Composite Pattern

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

The Composite Design Pattern

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

Object-Oriented Oriented Programming

Produced by. Design Patterns. MSc in Computer Science. Eamonn de Leastar

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Design Patterns IV Structural Design Patterns, 1

Design Patterns. Decorator. Oliver Haase

Design Patterns. Vasileios Theodorou

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

Considerations. New components can easily be added to a design.

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

SDC Design patterns GoF

Design Patterns. Definition of a Design Pattern

Laboratorio di Sistemi Software Design Patterns 2

Design Patterns in C++

CS 2340 Objects and Design

Tecniche di Progettazione: Design Patterns

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Introduction and History

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

Topics in Object-Oriented Design Patterns

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

CSE 431S Type Checking. Washington University Spring 2013

Figure 1. A breadth-first traversal.

Composite Pattern. IV.4 Structural Pattern

Design Patterns Reid Holmes

Tecniche di Progettazione: Design Patterns

Tecniche di Progettazione: Design Patterns

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Design Patterns Reid Holmes

Acknowledgements. Materials were borrowed from

» Access elements of a container sequentially without exposing the underlying representation

Principles of Software Construction: Objects, Design, and Concurrency. Part 1: Design for reuse. Design patterns for reuse

Java Collection Framework

Outline. Composite Pattern. Model-View-Controller Pattern Callback Pattern

Working with Mediator Framework

BFH/HTA Biel/DUE/Course 355/ Software Engineering 2

Design Patterns (Composite)

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns Lecture 2

Tecniche di Progettazione: Design Patterns

Tecniche di Progettazione: Design Patterns

Tecniche di Progettazione: Design Patterns

Software Engineering I (02161)

Outline. Purpose. Useful Definitions. Pattern Overview

BFH/HTA Biel/DUE/Course 355/ Software Engineering 2

Software Quality Management

04/06/2013. Study Goals. Design Patterns in AspectJ. Evaluation Criteria. Problems with OO Solution

We hope that this paper will give you a good starting point on how to read, learn, find and use design patterns. After reading the whole example, you

CISC 322 Software Architecture

AP COMPUTER SCIENCE AB 2006 SCORING GUIDELINES

Motivating Problem (2) Design for tree structures with whole-part hierarchies. The Composite Design Pattern

Design Patterns. An introduction

Tecniche di Progettazione: Design Patterns

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Design Patterns Revisited

The GoF Design Patterns Reference

Génie Logiciel et Gestion de Projets. Patterns

Tecniche di Progettazione: Design Patterns

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

Study Goals. Evaluation Criteria. Problems with OO Solution. Design Pattern Implementation in Java and AspectJ

Design Patterns (Facade, Composite)

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design patterns. OOD Lecture 6

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Tecniche di Progettazione: Design Patterns

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Iterator Design Pattern

Mixed projects: Java + Kotlin. Svetlana Isakova

2.1 Design Patterns and Architecture (continued)

JAVA Programming Language Homework I - OO concept

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

Tecniche di Progettazione: Design Patterns

Tecniche di Progettazione: Design Patterns

Trees. Trees. CSE 2011 Winter 2007

CS 3114 Data Structures and Algorithms DRAFT Project 2: BST Generic

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

3. Task Group & Applying Composite Pattern

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Unit5: Packages and abstraction

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. Inheritance Hierarchy. The Idea Behind Inheritance

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

1)Write a program on inheritance and check weather the given object is instance of a class or not?

2.1 Design Patterns and Architecture (continued)

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

LOG6306 : Études empiriques sur les patrons logiciels

Course Content. Objectives of Lecture 24 Inheritance. Outline of Lecture 24. CMPUT 102: Inheritance Dr. Osmar R. Zaïane. University of Alberta 4

About this sample exam: Learning Goals

Design Patterns V Structural Design Patterns, 2

Transcription:

Tecniche di Progettazione: Design Patterns GoF: Composite 1

Composite pattern Intent Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. This is called recursive composition. Applicability Use the Composite 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: structure {ordered 1..*

Composite: participants Component declares the interface for object composition implements default behaviour (if any) declares an interface for accessing and managing the child components Leaf Defines the behaviour of the composition primitive objects Composite: defines behaviour for components having children stores child components implements operations to access childs Client: manipulates objects in the composition through the Composite interface

Composite: Example

Composite: Collaboration

Directory / File Example Directory = Composite File = Leaf / bin/ user/ tmp/ file 1 file 2 subdir/ file 3 file 4 file5 Composit e Composit e Composit e Composit e Leaf Leaf Composit Leaf e Leaf Leaf

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: File Composite Class: Directory

Directory / File Example Structure Abstract Base Class: Node Leaf Class: File Composite Class: Directory

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 subdirectories long Directory::size () { long total = 0; Node* child; for (int i = 0; child = getchild(); ++i; { total += child->size(); return total;

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.

Implementation Issues Should Component maintain the list of components that will be used by a composite object? That is, should this list be an instance variable of Component rather than Composite? Better to keep this part of Composite and avoid wasting the space in every leaf object. Where should the child management methods (add(), remove(), getchild()) be declared? In the Component class: Gives transparency, since all components can be treated the same. But it's not safe, since clients can try to do meaningless things to leaf components at run-time. In the Composite class: Gives safety, since any attempt to perform a child operation on a leaf component will be caught at compile-time. But we lose transparency, since now leaf and composite components have different interfaces. 12

Implementation Issues cont d Is child ordering important? Depends on application What's the best data structure to store components? Depends on application A composite object knows its contained components, that is, its children. Should components maintain a reference to their parent component? Depends on application, but having these references supports the Chain of Responsibility pattern 13

Complex hierarchy of menu items

Operations applied to whole or parts

Some observations The print menu method in the MenuComponent class is recursive. Now lets look at an alternative implementation which uses an iterator to iterate through composite classes the composite iterator

MenuComponent public abstract class MenuComponent { public void add(menucomponent menucomponent) { throw new UnsupportedOperationException(); public void remove(menucomponent menucomponent) { throw new UnsupportedOperationException(); public MenuComponent getchild(int i) { The composite methods throw new UnsupportedOperationException(); public String getname() { throw new UnsupportedOperationException(); 20

MenuComponent public String getdescription() { throw new UnsupportedOperationException(); public double getprice() { throw new UnsupportedOperationException(); public boolean isvegetarian() { throw new UnsupportedOperationException(); public void print() { throw new UnsupportedOperationException(); 21

MenuItem public class MenuItem extends MenuComponent { String name; String description; boolean vegetarian; double price; public MenuItem(String name, String description, boolean vegetarian, double price){ this.name = name; this.description = description; this.vegetarian = vegetarian; this.price = price; 22

MenuItem public String getname() { return name; public String getdescription() { return description; public double getprice() { return price; public boolean isvegetarian() { return vegetarian; public void print(){ System.out.print(" " + getname()); if (isvegetarian()) System.out.print("(v)"); System.out.println(", " + getprice()); System.out.println(" -- " + getdescription()); 23

Menu public class Menu extends MenuComponent { ArrayList menucomponents = new ArrayList(); String name; String description; public Menu(String name, String description) { this.name = name; this.description = description; public void add(menucomponent menucomponent) { menucomponents.add(menucomponent); 24

Menu public void remove(menucomponent menucomponent) { menucomponents.remove(menucomponent); public MenuComponent getchild(int i) { return (MenuComponent)menuComponents.get(i); public String getname() { return name; public String getdescription() { return description; public void print() { System.out.print("\n" + getname()); System.out.println(", " + getdescription()); Iterator iterator = menucomponents.iterator(); while (iterator.hasnext()) { MenuComponent menucomponent = (MenuComponent)iterator.next(); menucomponent.print(); 25

Related Patterns Chain of Responsibility Component-Parent Link Decorator When used with composite will usually have a common parent class. So decorators will need to support the component interface with operations like: Add, Remove, GetChild. Flyweight Lets you share components, but they can no longer reference their parents. Iterator Can be used to traverse composites. Visitor Localizes operations and behavior that would otherwise be distributed across composite and leaf classes.

Homework: composite and iterator Using an iterator over a composite, let print all the menus, adding indentation. E.g. PANCAKE HOUSE MENU K&B's Pancake Breakfast, Pancakes with scrambled eggs, and toast Regular Pancake Breakfast, Pancakes with fried eggs, sausage CAFE MENU Coffee Cake, Crumbly cake topped with cinnamon and walnuts DINER MENU Vegetarian BLT, (Fakin') Bacon with lettuce & tomato on whole wheat DESSERT MENU Apple Pie, Apple pie with a flakey crust, topped with vanilla icecream Cheesecake, Creamy New York cheesecake, with a chocolate graham crust 27