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

Slides:



Advertisements
Similar presentations
Graphical User Interfaces (Part IV)
Advertisements

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.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphical User Interface (GUI) Programming III. Lecture Objectives Exploring more GUI programming elements in Java Using labels in GUIs Using colors to.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
FSA for tennis scorekeeping B WINS A WINS Adv B Adv A Deuce start A B A A A A A A A A A.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
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.
1 L46 Advanced GUI Component (1). 2 OBJECTIVES  To create and manipulate sliders, and menus,
Advanced Java Class GUI, part 2. JComponent methods addXXXListener(XXXListener) repaint() – [optional arguments: delay and coordinates of sub-area to.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
More on Creating GUIs in Java using Swing David Meredith Aalborg University.
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)
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
Introduction to Java Swing “We are the sultans of swing” – Mark Knopfler.
Composite Design Pattern. Motivation – Dynamic Structure.
Design Patterns and Graphical User Interfaces Horstmann ,
Inheritance Abstract Classes Check out Inheritance from SVN.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
7-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : User Interface Components with.
3-1 Composite Design Pattern Rick Mercer. 2 Composite Pattern Context : –Often complex structures are built with container and primitive objects. Container.
CS377A: A Programming Approach to HCI Jan Borchers Spring Swing Refresher David Merrill 5/14/2002
 2002 Prentice Hall, Inc. All rights reserved Introduction Graphical User Interface (GUI) –Gives program distinctive “look” and “feel” –Provides.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
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.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Graphical User Interfaces (Part 2) 1. View  view  presents the user with a sensory (visual, audio, haptic) representation of the model state  a user.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
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.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
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.
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.
CS 415 N-Tier Application Development By Umair Ashraf June 25,2013 National University of Computer and Emerging Sciences Lecture # 4 Web Presentation Patterns.
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.
1 Chapter 16: Creating Windows. 2 Basics of GUI Programming How to create a Window. TryWindow.java You always need a JFrame component before creating.
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.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 15 : Swing III King Fahd University of Petroleum & Minerals College of Computer.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Visual C++ Programming: Concepts and Projects Chapter 10B: Recursion (Tutorial)
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)
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Chapter 6 Building Java GUIs. MVC Model View Controller The model passes its data to the view for rendering The view determines which events are passed.
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Advanced User Interfaces
Lecture 27 Creating Custom GUIs
Java Programming: From Problem Analysis to Program Design,
Lecture 28 Concurrent, Responsive GUIs
Iterator and Composite Design Patterns
Jim Fawcett CSE776 – Design Patterns Summer 2003
Composite Pattern Context:
Graphical User Interfaces in Java Event-driven programming
Constructors, GUI’s(Using Swing) and ActionListner
Presentation transcript:

3-1 Composite Design Pattern Rick Mercer

2 Composite Pattern Recurring problem: –Often complex structures are built with container and primitive objects. Container objects can contain other objects –How can code that uses 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 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: it is a primitive Composite: Defines behavior for components having children –Also implements child-related operations of Component

5 Participants Component has operations that apply to all –The component can be a Composite or a Leaf Composite adds methods indicating a collection: a dd(), and r emove() In each method, a Component is passed –Can add either a Child or a Component Component should not add itself Should not add a Component to a leaf

Usage Example ArrayList a = new ArrayList (); a.add("abc"); a.add("cde"); ArrayList b = new ArrayList (); b.add(1.11); b.add(2.22); 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 here? ___________________ What type is the Composite? ___________________ What type is the Component? ___________________ Output? ________________________________

7 Use Example: Java Swing Java Swing has four major pieces: –Events and EventListeners –Layouts –Drawing –Graphical Components The root of all is also named Component Component utilizes the Composite pattern in several ways –One you may find useful or need for your final project

8 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

9 JMenuItem menu = new JMenu("Composite"); menu.setMnemonic('C');//Open with alt-C // Create two leafs 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);

10 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); Run JMenuItemDemoComposite.java See code demo page

Portfolios in Portfolios 11

Portfolio overall = new Portfolio("My Entire Retirement Portfolio"); // Add two leafs to "my IRA" Stock aStock = new Stock("Seven Eleven", 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 sun = new Stock("Sun", 20, 30.50); MutualFund highRisk = new MutualFund("Nasdaq", 13, 45.20); tech.add(sun); // 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 a 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); 12

Create a print method for this 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 China with value $