1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.

Slides:



Advertisements
Similar presentations
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
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)
Fall 2007CS 225 Introduction to Software Design Chapter 1.
Chapter 8, Object Design Introduction to Design Patterns
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
7M701 1 Class Diagram advanced concepts. 7M701 2 Characteristics of Object Oriented Design (OOD) objectData and operations (functions) are combined 
Design Patterns. CS351 - Software Engineering (AY2007)Slide 2 Behavioral patterns Suppose we have an aggregate data structure and we wish to access the.
K. Stirewalt CSE 335: Software Design Synthetic OO Design Concepts & Reuse Lecture 2: Abstract classes and composite structures Topics: –Elaboration of.
Requirements Analysis 2 What objects collaborate to achieve the goal of a use case?
Visual Basic: An Object Oriented Approach 3 – Making Objects Work.
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.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 20: Multiple inheritance.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
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.
Ranga Rodrigo. Class is central to object oriented programming.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
Design Patterns.
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design Adapter Pattern Façade pattern.
Department of Computer Science, York University Object Oriented Software Construction 16/09/ :52 PM 0 COSC3311 – Software Design Decorator Pattern.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
Putting together a complete system Chapter 10. Overview  Design a modest but complete system  A collection of objects work together to solve a problem.
Introduction to Software Design Chapter 1. Chapter Objectives  To become familiar with the software challenge and the software life cycle  To understand.
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,
Department of Computer Science, York University Object Oriented Software Construction 13/10/ :44 AM 0 CSE3311 – Software Design Adapter Pattern.
1 Dept. of Computer Science & Engineering, York University, Toronto CSE3311 Software Design State Pattern.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
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.
Object Oriented Software Development
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
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.
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.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
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.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Slide design: Dr. Mark L. Hornick
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Software Design and Architecture
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
Design Pattern Detection
Jim Fawcett CSE776 – Design Patterns Summer 2003
Software Development CSE3311
Composite Pattern Context:
13. Composite Pattern SE2811 Software Component Design
Software Design Lecture : 35.
13. Composite Pattern SE2811 Software Component Design
Composite Design Pattern By Aravind Reddy Patlola.
Software Design Lecture : 36.
Visitor Pattern Intent
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern

2 Problem  Many manufactured systems, such as computer systems or stereo systems, are composed of individual components and sub-systems that contain components.  For example, a computer system can have cabinets that contain various types of chassis that (in turn) contain components (hard-drive chassis, power- supply chassis) and busses that contain cards.  The entire system is composed of individual pieces of equipment (hard drives, cd-rom drives), as well as composites such as cabinets, busses and a chassis.  Each piece of equipment has properties such as power-consumption and cost. Design a system that will allow us to easily build systems and calculate their total cost and power consumption.

3 CABINET HARD_DRIVE CARD CHASSIS POWER_ SUPPLY DVD-CDROM Tree structures with part-whole hierarchies

4 price power_consumption

5 Composite Pattern  Compose objects into tree structures that represent whole-part hierarchies. Composites let clients treat individual objects (leafs) and compositions (nodes) of objects uniformly.  We can then apply the same operation (e.g. calculate price or power consumption) over both composites and individual objects (i.e. we can ignore the difference between leafs and nodes).

6 price: VALUE add(child: EQUIPMENT) children: LIST[EQUIPMENT] The client uses abstract class EQUIPMENT to manipulate objects in the composition. Class EQUIPMENT defines an interface for all objects in the composition: both the composite and leaf nodes. May implement default behavior for add(child) etc. Class COMPOSITE ‘s role is (a) implement leaf related ops such as price and (b) to define component behaviour such as storing a child. A leaf has no children. Note that the leaf also inherits features like children and add that don’t necessarily make all that sense for a leaf node.

7 Cleaner solution – Multiple Inheritance Put the price & power consumption behavior here Put the tree behavior such as adding a child and list of children here where it is needed

8 class COMPOSITE [T] feature children : LIST [T] add (new_child: T) require new_child /= Void do children.extend(new_child) ensure has (new_child) remove (child: T) require child /= Void ensure not has (child) has (child: T): BOOLEAN -- does 'child' belong to the composite? require child /= Void invariant children_not_void: children /= Void end

9 deferred class EQUIPMENT feature make (its_name: STRING) discount_price: REAL name: STRING net_price, discount_price: REAL power: REAL invariant name_not_void: name /= Void positive_power: power >= 0.0 positive_price: discount_price >= 0.0 real_discount: net_price >= discount_price end

10 class COMPOSITE_EQUIPMENT inherit COMPOSITE [EQUIPMENT] EQUIPMENT feature net_price : REAL -- Sum the net prices of the subequipments local i : INTEGER do from i := 1 until i > children.count loop Result := Result + children[i].net_price i := i + 1 end -- loop end … end

11 class MAIN create make feature e : EQUIPMENT make local cabinet : CABINET -- Will hold a CHASSIS chassis : CHASSIS -- Will contain a BUS and a DISK_DRIVE bus : BUS -- Will hold a CARD do create cabinet.make("PC Cabinet"); create chassis.make("PC Chassis") cabinet.add(chassis) create bus.make("MCA Bus"); create {CARD}e.make("16Mbs Token Ring"); bus.add(e); chassis.add(bus) create{DISK_DRIVE}e.make("500 GB harddrive"); chassis.add(e) print("The net price is ") print(cabinet.net_price); io.put_new_line end -- make end -- MAIN Cabinet Chassis CARD BUSDISK

12 Contracts  How would you write the contracts? price: power_consumption MSL_SEQ[G -> NUMERIC] sum_all e in composite e.price

13 Non-software analogy  Arithmetic expressions are composites  Each arithmetic expression has  An operand  An operator such as + - * /  Another operand  Each operand may be another arithmetic expression  Valid arithmetic expressions   (2 + 3) + (4 * 6)

14 Whole-Part Hierarchy Example DIAGRAM TEXTLINE DIAGRAM OVALTEXT DIAGRAM OVALTEXT

15 Composite – Example Architecture LINE + draw + DIAGRAM + draw + add remove GRAPHIC * draw * TEXT + draw + OVAL + draw + CLIENT

16 Composite – Alternative Architecture LINE + draw + DIAGRAM + draw + add + remove + GRAPHIC * draw * TEXT + draw + OVAL + draw + COMPOSITE[T] * add * remove * CLIENT TT

17 Composite – Participants  Component Defines generic operations on entities  Leaf Component Implements properties of a primitive entity  Composite Declares operations on collections  Composite Component Combines operations of collections and components  Client Uses components (leaf or composite)

18 Composite – Abstract Architecture COMPOSITE_COMPONENT + op_1 + op_2 + add + remove + COMPONENT * op_1 * op_2 * COMPOSITE[T] * add * remove * CLIENT TT LEAF + op_1 + op_2 +

19 Composite – Consequences  Whenever client expects a primitive it can accept a composite  Client is simplified by removing tag-case statements to identify parts of the composition  Easy to add new components by subclassing, client does not change  If compositions are to have restricted sets of components, have to rely on run-time checking

20 Composite real-world examples  java.awt.Component is a class representing objects that have a graphical representation that can be displayed on the screen and that can interact with the user [COMPONENT]  java.awt.Container represents components that can contain other components [COMPOSITE]  Contains methods add(Component) and remove(Component)  Example subclasses: Panel, Window  java.awt.Button is a component that does not contain other components [LEAF]

21 Questions  Write out complete code for the equipment example  How would you contract it.