3-1 Composite Design Pattern Rick Mercer. 2 Composite Pattern Context : –Often complex structures are built with container and primitive objects. Container.

Slides:



Advertisements
Similar presentations
1 Introduction to Computation and Problem Solving Class 24: Case Study: Building a Simple Postfix Calculator Prof. Steven R. Lerman and Dr. V. Judson Harward.
Advertisements

CE881: Mobile and Social Application Programming Simon M. Lucas Menus and Dialogs.
Graphical User Interfaces (Part IV)
Containers and Components 1.Containers collect GUI components 2.Sometimes, want to add a container to another container 3.Container should be a component.
Fall 2007CS 225 Graphical User Interfaces Event Handling Appendix C.
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)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Software Design and Documentation Individual Presentation: Composite Pattern 9/11/03.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
GUI and Event-Driven Programming Part 2. Event Handling An action involving a GUI object, such as clicking a button, is called an event. The mechanism.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Graphic User Interfaces Part 1. Typical GUI Screen from Microsoft Word What GUI “components” can you see? –Menus? Buttons? Labels? What else? –Anything.
Composite Design Pattern. Motivation – Dynamic Structure.
Design Patterns and Graphical User Interfaces Horstmann ,
Inheritance Abstract Classes Check out Inheritance from SVN.
7-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : User Interface Components with.
SE2811 Week 7, Class 1 Composite Pattern Applications Conceptual form Class structure Coding Example Lab Thursday: Quiz SE-2811 Slide design: Dr. Mark.
CS377A: A Programming Approach to HCI Jan Borchers Spring Swing Refresher David Merrill 5/14/2002
3-1 Composite Design Pattern Rick Mercer. 2 Composite Pattern Recurring problem: –Often complex structures are built with container and primitive objects.
Chapter 11 Arrays Continued
CSE 219 Patterns in Programming More Design Patterns.
Strategy Design Patterns CS 590L - Sushil Puradkar.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
SWING IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
Software Design 5.1 From Using to Programming GUIs l Extend model of "keep it simple" in code to GUI  Bells and whistles ok, but easy to use and hide.
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Field Trip #22 Creating an Excel Spreadsheet with Java By Keith Lynn.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Swing - 2 Session 13. Swing - 2 / 2 of 38 Objectives (1) Discuss trees and tables Discuss progress bars Discuss MVC architecture Describe menus.
CS212: Object Oriented Analysis and Design Lecture 39: Design Pattern-III.
5-Jan-16 R Ramesh Swing. 5-Jan-16 R Ramesh An Introduction to Java Foundation Classes (JFC) A suite of libraries to assist programmers create enterprise.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Computer Science [3] Java Programming II - Laboratory Course Lab 4 -1 : Introduction to Graphical user interface GUI Components Faculty of Engineering.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user interface element (the user interface.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Today Review passing by reference and pointers. null pointers. What is an Object? Winter 2016CMPE212 - Prof. McLeod1.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Lecture 27 Creating Custom GUIs
Composite Design Pattern
Java Programming: From Problem Analysis to Program Design,
Iterator and Composite Design Patterns
Jim Fawcett CSE776 – Design Patterns Summer 2003
Composite Pattern Context:
Behavioral Patterns Part-I introduction UNIT-VI
Presentation transcript:

3-1 Composite Design Pattern Rick Mercer

2 Composite Pattern Context : –Often complex structures are built with container and primitive objects. Container objects can contain other objects –How can code using these classes treat all the objects in the structure identically sometimes, yet differently when it matters? Solution : –Define an abstract class that represents primitives and containers Composite was used in the View class of Smalltalk MVC as well as most other GUI toolkits

3 General Form of Composite

4 Participants abstract class Component –Declares the interface for all objects in the composition –Implements default behavior, as appropriate –Declares an algorithm interface (set of methods) for accessing and managing child components Leaf: Has no children. These are primitives Composite: Defines behavior for components having children such as add, remove

5 Participants In each method, a Component is passed –Use the object to which any of the others can be assigned Should not be able to add a Component to a leaf A Component should not add itself to itself

File Systems Directories contain entries, each of which could be a directory

Abstract Composite An arithmetic expression consists of an operand, an operator (+ - * /), and another operand. –The operand can be a number, or another arithmetic expression

A Collection ArrayList a = new ArrayList (); a.add("abc"); a.add(123); ArrayList b = new ArrayList (); b.add(1.11); b.add(true); System.out.println("a: " + a); System.out.println("b: " + b); b.add(a); b.add(b); // a.add(b); Stack Overflow System.out.println("a: " + a); System.out.println("b: " + b); What types are the Leafs? What type is the Composite? What type is the Component? Output? String, Integer, Double, Boolean ArrayList Object a: [abc, 123] b: [1.11, true] a: [abc, 123] b: [1.11, true, [abc, 123], (this Collection)]`

9 Use Example: Java Swing Java Swing has four major pieces: –Events and EventListeners –Layouts –Drawing –Graphical Components The root of all of these is named: Component Component utilizes the Composite pattern in several ways, with menus for example –One you may find useful or need for your projects

JMenus in Java Swing Java menus use the Composite Design Pattern JMenuBar is a c omposite extending JComponent JComponent JMenuBar is a c omposite extending JComponent –Can add others like JLabel, JTextField –Can also add JMenuItem to JMenuItem JMenuItem has three subclasses –JMenu –JRadioButtonMenuItem –JCheckboxMenuItem

Can add to any Container

12 JMenuItem menu = new JMenu("Composite"); // Add two items JLabel label = new JLabel("Label"); JTextField textF = new JTextField("text field"); menu.add(label); menu.add(textF); // Add a Composite JMenuItem menuItem = new JMenuItem("menu item"); menu.add(menuItem); // Add two Composites to a Composite JMenuItem jmi1Nest = new JMenu("Nest 1"); menu.add(jmi1Nest); JMenuItem jmiNested1 = new JMenuItem("Nested in 1"); jmi1Nest.add(jmiNested1); JMenuItem jmiNested2 = new JMenuItem("Nested in 1 also"); jmi1Nest.add(jmiNested2);

JMenuItemDemoComposite // Add two more Composites JMenuItem checkBox = new JCheckBoxMenuItem("Human", false); JMenuItem radioButton = new JRadioButtonMenuItem("Computer", true); menu.add(checkBox); menu.add(radioButton); // Add two more Composites JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); menuBar.add(menu);

Portfolios in Portfolios

Portfolio overall = new Portfolio("My IRA"); // Add two leafs to "my IRA" Stock aStock = new Stock(”Star Lucks", 500, 9.15); overall.add(aStock); BankAccount account = new BankAccount("Swiss Account", ); overall.add(account); // Create a tech portfolio Portfolio tech = new Portfolio("Tech Stocks"); Stock oracle = new Stock(”Oracle", 20, 30.50); MutualFund highRisk = new MutualFund("Nasdaq", 13, 45.20); tech.add(oracle); // add leaf tech.add(highRisk); // add leaf // Add this 2nd portfolio to the overall portfolio overall.add(tech); // add Composite // overall.add(overall); There is an if to avoid adding this to this // Create an overseas portfolio of tech stocks Portfolio global = new Portfolio("Global Equities"); Stock pacificRim = new Stock("Pacific Rim Tech", 10, 12.34); MutualFund lrgGrow = new MutualFund("Large Growth", 100, 95.21); global.add(pacificRim); global.add(lrgGrow); tech.add(global);

Recursive toString result My Entire Retirement Portfolio with value $ shares of Seven Eleven with value $ BankAccount: 'Swiss Account' with value $ Tech Stocks with value $ shares of Sun with value $ shares of Nasdaq with value $587.6 Global Equities with value $ shares of Pacific Rim Tech with value $ shares of Large Growth with value $9521.0

Summary Composite lets clients treat individual objects and compositions of objects uniformly Composite relies on recursive composition to organize an open-ended number of objects Composite avoids the undesirable of having to query the "type" of each object before processing