Tecniche di Progettazione: Design Patterns

Similar documents
Tecniche di Progettazione: Design Patterns

Tecniche di Progettazione: Design Patterns

Design Pattern: Composite

The Composite Pattern

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

Design Patterns IV Structural Design Patterns, 1

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

The Composite Design Pattern

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

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

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

Object-Oriented Oriented Programming

Design Patterns in C++

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

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

Laboratorio di Sistemi Software Design Patterns 2

Topics in Object-Oriented Design Patterns

CS 2340 Objects and Design

SDC Design patterns GoF

Working with Mediator Framework

Design Patterns. Vasileios Theodorou

Figure 1. A breadth-first traversal.

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

Design Patterns Reid Holmes

Tecniche di Progettazione: Design Patterns

Design Patterns. Decorator. Oliver Haase

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

Tecniche di Progettazione: Design Patterns

Introduction and History

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

CISC 322 Software Architecture

3. Task Group & Applying Composite Pattern

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

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

Composite Pattern. IV.4 Structural Pattern

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

CSE 431S Type Checking. Washington University Spring 2013

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

Concurrent Models of Computation for Embedded Software

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Tecniche di Progettazione: Design Patterns

Design Patterns (Composite)

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

Design Patterns. Definition of a Design Pattern

Tecniche di Progettazione: Design Patterns

Génie Logiciel et Gestion de Projets. Patterns

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

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

Software Quality Management

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

CSIT5300: Advanced Database Systems

CSC 467 Lecture 13-14: Semantic Analysis

INHERITED BEHAVIOUR PATTERN

Design Patterns V Structural Design Patterns, 2

Trees. (Trees) Data Structures and Programming Spring / 28

Design Patterns Reid Holmes

Tecniche di Progettazione: Design Patterns

Design patterns. OOD Lecture 6

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Tecniche di Progettazione: Design Patterns

Data Structures and Algorithms

Design Patterns. An introduction

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

Data Structure - Binary Tree 1 -

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

Chapter 11: Indexing and Hashing" Chapter 11: Indexing and Hashing"

CSC148 Week 6. Larry Zhang

Adapter pattern. Acknowledgement: Freeman & Freeman

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

CSE 214 Computer Science II Introduction to Tree

Chapter 6 Introduction to Defining Classes

C++ Important Questions with Answers

Project C: Genetic Algorithms

Design Patterns (Facade, Composite)

CHAIN OF RESPONSIBILITY (DP 223)

CSE 230 Intermediate Programming in C and C++ Binary Tree

Transitioning from Spread 3.17.x to Spread 4.0

Chapter B: Hierarchical Model

Accessing Arbitrary Hierarchical Data

Laboratorio di Progettazione di Sistemi Software Design Pattern Creazionali. Valentina Presutti (A-L) Riccardo Solmi (M-Z)

Tecniche di Progettazione: Design Patterns

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

Chapter 11: Indexing and Hashing

Selected Design Patterns

Design Patterns. Collectional Patterns. Session objectives 11/06/2012. Introduction. Composite pattern. Iterator pattern

Adapter Pattern Structural

Tecniche di Progettazione: Design Patterns

The Object Recursion Pattern

CSE 421/521 - Operating Systems Fall Lecture - XIX. File Systems. University at Buffalo

LTSA User Manual.

InterPARES 2 Project

Alternator. An Object Behavioral Design Pattern. John Liebenau

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Code Coverage. Copyright 2009 JADE Software Corporation Limited. All rights reserved.

Tecniche di Progettazione: Design Patterns

Verifiable Hierarchical Protocols with Network Invariants on Parametric Systems

Computer (Literacy) Skills. Variables, records, and pointers. Lubomír Bulej KDSS MFF UK

CSE 530A. B+ Trees. Washington University Fall 2013

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/ / file1 file2 subdir/ file3 file4 file5 Composite Composite Composite Composite Leaf Leaf Composite Leaf 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