COMPOSITE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.

Slides:



Advertisements
Similar presentations
1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Advertisements

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.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Visual Basic: An Object Oriented Approach 11 – Patterns in object oriented programming.
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.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
More OOP Design Patterns
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
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.
The Design of JUnit Yonglei Tao. Test-First Development  An essential element in eXtreme Programming (XP)  Test is written before the code  As an executable.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Composit Pattern.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Design Patterns – II Lecture IV. Singleton Pattern Intent – Ensure a class only has one instance, and provide a global point of access to it Motivation.
Computing IV Singleton Pattern Xinwen Fu.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
1 Design Patterns Object-Oriented Design. 2 Design Patterns 4Reuse of design knowledge and experience 4Common in many engineering disciplines 4Avoids.
Creational Patterns
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
Proxy.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
The "8 Queens" problem Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen. What are the "choices"?
Design Patterns Introduction
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Lecture 14 Inheritance vs Composition. Inheritance vs Interface Use inheritance when two objects share a structure or code relation Use inheritance when.
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.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
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.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
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.
Design Patterns Spring 2017.
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Composite Design Pattern
Design Patterns with C# (and Food!)
Composite Pattern SE2811 Software Component Design
Iterator and Composite Design Patterns
Design Patterns - A few examples
Software Engineering Lecture 7 - Design Patterns
Jim Fawcett CSE776 – Design Patterns Summer 2003
Interpreter Pattern.
Chapter 8, Design Patterns Composite
13. Composite Pattern SE2811 Software Component Design
13. Composite Pattern SE2811 Software Component Design
Software Design Lecture : 39.
CIS 644 Tues. Nov. 30, 1999 W15A … patterns.
Software Design Lecture 10.
Presentation transcript:

COMPOSITE

Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory Builder Prototype Singleton Adapter Bridge Composite Facade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento State Strategy Visitor Observer

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.

Structure

Participants  Component  declares the interface for object composition  implements default behaviour  declares an interface for accessing and managing the child components  Leaf  represents leaf objects in the composition  Composite  defines behaviour for components having children  stores child components  implements child-related operations in the Component interface  Client  manipulates objects in the composition through the Composite interface

Consequences  Benefits  It makes it easy to add new kinds of components  It makes clients simpler, since they do not have to know if they are dealing with a leaf or a composite component

Example import java.util.*; interface Component { public String defaultMethod(); public ArrayList getChildren(); public boolean addComponent(Component c); public boolean removeComponent(Component c); }

class Composite implements Component { private String id; private ArrayList components = new ArrayList (); public Composite(String identification) { id = identification; } public String defaultMethod() { String s = "(" + id + ":"; for (Component child : getChildren()) s = s + " " + child.defaultMethod(); return s + ")"; } public ArrayList getChildren() { return components; } public boolean addComponent(Component c) { return components.add(c); } public boolean removeComponent(Component c) { return components.remove(c); }

class Leaf implements Component { private String id; public Leaf(String identification) { id = identification; } public String defaultMethod() { return id; } public ArrayList getChildren() { return null; } public boolean addComponent(Component c) { return false; } public boolean removeComponent(Component c) { return false; } }

class CompositePattern { public static void main(String[] args) { Composite england = new Composite("England"); Leaf york = new Leaf("York"); Leaf london = new Leaf("London"); england.addComponent(york); england.addComponent(london); england.removeComponent(york); Composite france = new Composite("France"); france.addComponent(new Leaf("Paris")); Composite europe = new Composite("Europe"); europe.addComponent(england); europe.addComponent(france); System.out.println( europe.defaultMethod() ); }

Composite Example - Motivation  GUI Windows and GUI elements  How does the window hold and deal with the different items it has to manage?  Widgets are different than WidgetContainers

Bad News Implementation class Window { Buttons[] myButtons; Menus[] myMenus; TextAreas[] myTextAreas; WidgetContainer[] myContainers; public void update() { if ( myButtons != null ) for ( int k = 0; k < myButtons.length(); k++ ) myButtons[k].refresh(); if ( myMenus != null ) for ( int k = 0; k < myMenus.length(); k++ ) myMenus[k].display(); if ( myTextAreas != null ) for ( int k = 0; k < myButtons.length(); k++ ) myTextAreas[k].refresh();

Bad News Implementation if ( myContainers != null ) for ( int k = 0; k < myContainers.length(); k++ ) myContainers[k].updateElements(); etc. } public void fooOperation() { if ( blah ) etc. if ( blah ) etc. }

The Composite Pattern Component implements default behavior for widgets when possible Button, Menu, etc overrides Component methods when needed WidgetContainer will have to overrides all widgetOperations

Implementation Issues  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

Explicit Parent References  Aid in traversing the structure class WidgetContainer { Component[] myComponents; public void update() { if ( myComponents != null ) for ( int k = 0; k < myComponents.length(); k++ ) myComponents[k].update(); } public add( Component aComponent ) { myComponents.append( aComponent ); aComponent.setParent( this ); }

Explicit Parent References class Button extends Component { private Component parent; public void setParent( Component myParent) { parent = myParent; } etc. }

Implementation Issues  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.

Transparent vs. Safe

Applicability  Use 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

Java Use of Composite - AWT Widgets

Another Example

Problem  Each of the company members receives a salary, and we could at any time ask for the cost of any employee to the company.  We define the cost as the salary of that person and those of all his subordinates. Solution  Here is an ideal example for a composite:  The cost of an individual employee is simply his salary (and benefits).  The cost of an employee who heads a department is his salary plus those of all his subordinates.

 We would like a single interface that will produce the salary totals correctly whether the employee has subordinates or not. public float getSalaries();  At this point, we realize that the idea of all Composites having the same standard interface is probably naïve.  We’d prefer that the public methods be related to the kind of class we are actually developing.  So rather than have generic methods like getValue(), we’ll use getSalaries().

The Employee Class public class Employee { //Our Employee class will store the name and salary of each employee, and String name; // allow us to fetch them as needed. float salary; Vector subordinates; // public Employee(String _name, float _salary) { name = _name; salary = _salary; subordinates = new Vector(); } // public float getSalary() { return salary; } // public String getName() { return name; }

public void add(Employee e) { subordinates.addElement(e); } // public void remove(Employee e) { subordinates.removeElement(e); } public Enumeration elements() { return subordinates.elements(); }

public float getSalaries() { float sum = salary; //this one’s salary //add in subordinates salaries for(int i = 0; i < subordinates.size(); i++) { sum += ((Employee)subordinates.elementAt(i)).getSalaries(); return sum; }

Building the Employee Tree boss = new Employee("CEO", ); boss.add(marketVP = new Employee("Marketing VP", )); boss.add(prodVP = new Employee("Production VP", )); marketVP.add(salesMgr = new Employee("Sales Mgr", 50000)); marketVP.add(advMgr = new Employee("Advt Mgr", 50000)); //add salesmen reporting to Sales Manager for (int i=0; i<5; i++) salesMgr.add(new Employee("Sales "+ new Integer(i).toString(), F +(float)(Math.random()-0.5)*10000)); advMgr.add(new Employee("Secy", 20000));

Building the Employee Tree prodVP.add(prodMgr = new Employee("Prod Mgr", 40000)); prodVP.add(shipMgr = new Employee("Ship Mgr", 35000)); //add manufacturing staff for (int i = 0; i < 4; i++) prodMgr.add( new Employee("Manuf "+ new Integer(i).toString(), F +(float)(Math.random()-0.5)*5000)); //add shipping clerks for (int i = 0; i < 3; i++) shipMgr.add( new Employee("ShipClrk "+ new Integer(i).toString(), F +(float)(Math.random()-0.5)*5000));

Visual JTree private void addNodes(DefaultMutableTreeNode pnode, Employee emp) { DefaultMutableTreeNode node; Enumeration e = emp.elements(); while(e.hasMoreElements()) { Employee newEmp = (Employee)e.nextElement(); node = new DefaultMutableTreeNode(newEmp.getName()); pnode.add(node); addNodes(node, newEmp); }

Cost (Sum of Salaries) public void valueChanged(TreeSelectionEvent evt) { //called when employee is selected in tree llist TreePath path = evt.getPath(); String selectedTerm = path.getLastPathComponent().toString(); //find that employee in the composite Employee emp = boss.getChild(selectedTerm); //display sum of salaries of subordinates(if any) if(emp != null) cost.setText(new Float(emp.getSalaries()).toString()); }

Restrictions on Employee Classes  e.g. never should have subordinates public void setLeaf(boolean b) { isLeaf = b; //if true, do not allow children } // public boolean add(Employee e) { if (! isLeaf) subordinates.addElement(e); return isLeaf; //false if unsuccessful }

Parent Reference  It is perfectly possible for any composite element to remember its parent by including it as part of the constructor: public Employee(Employee _parent, String _name, float _salary) { name = _name; //save name salary = _salary; //and salary parent = _parent; //and parent subordinates = new Vector(); isLeaf = false; //allow children }  This simplifies searching for particular members and moving up the tree when needed.