3-1 Composite Design Pattern Rick Mercer. 2 Composite Pattern Recurring problem: –Often complex...

13
3-1 Composite Design Pattern Rick Mercer

Transcript of 3-1 Composite Design Pattern Rick Mercer. 2 Composite Pattern Recurring problem: –Often complex...

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

3-1

Composite Design Pattern

Rick Mercer

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

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

containersComposite was used in the View class of Smalltalk MVC as well as most other GUI toolkits

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

3

General Form of Composite

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

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 primitiveComposite: Defines behavior for components having children– Also implements child-related operations of Component

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

5

Participants

Component has operations that apply to all– The component can be a Composite or a Leaf

Composite adds methods indicating a collection: add(), and remove() In each method, a Component is passed– Can add either a Child or a Component

Component should not add itselfShould not add a Component to a leaf

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

Usage ExampleArrayList<Object> a = new ArrayList<Object>();

a.add("abc");

a.add("cde");

ArrayList<Object> b = new ArrayList<Object>();

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?________________________________

________________________________

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

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

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

8

JMenus in Java Swing

Java menus use the Composite Design PatternJMenuBar is a composite extending JComponentJMenuBar is a composite extending JComponent– Can add others like JLabel, JTextField– Can also add JMenuItem to JMenuItem

JMenuItem has three subclasses– JMenu– JRadioButtonMenuItem– JCheckboxMenuItem

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

9

JMenuItem menu = new JMenu("Composite");menu.setMnemonic('C');//Open with alt-C// Create two leafsJLabel label = new JLabel("Label");JTextField textF = new JTextField("text field");menu.add(label);menu.add(textF);// Add a CompositeJMenuItem menuItem = new JMenuItem("menu item");menu.add(menuItem);// Add two Composites to a CompositeJMenuItem 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);

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

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

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

Portfolios in Portfolios

11

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

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", 300000); 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

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

Create a print method for this

My Entire Retirement Portfolio with value $315417.0 500 shares of Seven Eleven with value $4575.0 BankAccount: 'Swiss Account' with value $300000.0 Tech Stocks with value $10842.0 20 shares of Sun with value $610.0 13.0 shares of Nasdaq with value $587.6 Global Equities with value $9644.4 10 shares of Pacific Rim Tech with value $123.4 100.0 shares of Large Growth China with value $9521.0

13