Composite Design Pattern Rick Mercer 3 Composite Pattern

  • Slides: 13
Download presentation
Composite Design Pattern Rick Mercer 3 -

Composite Design Pattern Rick Mercer 3 -

Composite Pattern Recurring problem: – Often complex structures are built with container and primitive

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 2

General Form of Composite 3

General Form of Composite 3

Participants Component – Declares the interface for all objects in the composition – Implements

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 4

Participants Component has operations that apply to all – The component can be a

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 should not add itself Should not add a Component to a leaf 5

Usage Example Array. List<Object> a = new Array. List<Object>(); a. add("abc"); a. add("cde"); Array.

Usage Example Array. List<Object> a = new Array. List<Object>(); a. add("abc"); a. add("cde"); Array. List<Object> b = new Array. List<Object>(); What types are the Leafs b. add(1. 11); here? b. add(2. 22); __________ System. out. println("a: " + a); System. out. println("b: " + b); What type is the Composite? __________ b. add(a); What type is the Component? b. add(b); __________ // a. add(b); Stack Overflow Output? System. out. println("a: " + a); ______________ System. out. println("b: " + b); ________________

Use Example: Java Swing has four major pieces: – Events and Event. Listeners –

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

JMenus in Java Swing Java menus use the Composite Design Pattern JMenu. Bar is

JMenus in Java Swing Java menus use the Composite Design Pattern JMenu. Bar is a composite extending JComponent – Can add others like JLabel, JText. Field – Can also add JMenu. Item to JMenu. Item has three subclasses – JMenu – JRadio. Button. Menu. Item – JCheckbox. Menu. Item 8

JMenu. Item menu = new JMenu("Composite"); menu. set. Mnemonic('C'); //Open with alt-C // Create

JMenu. Item menu = new JMenu("Composite"); menu. set. Mnemonic('C'); //Open with alt-C // Create two leafs JLabel label = new JLabel("Label"); JText. Field text. F = new JText. Field("text field"); menu. add(label); menu. add(text. F); // Add a Composite JMenu. Item menu. Item = new JMenu. Item("menu item"); menu. add(menu. Item); // Add two Composites to a Composite JMenu. Item jmi 1 Nest = new JMenu("Nest 1"); menu. add(jmi 1 Nest); JMenu. Item jmi. Nested 1 = new JMenu. Item("Nested in 1"); jmi 1 Nest. add(jmi. Nested 1); JMenu. Item jmi. Nested 2 = new JMenu. Item("Nested in 1 also"); 9 jmi 1 Nest. add(jmi. Nested 2);

JMenu. Item. Demo. Composite // Add two more Composites JMenu. Item check. Box =

JMenu. Item. Demo. Composite // Add two more Composites JMenu. Item check. Box = new JCheck. Box. Menu. Item("Human", false); JMenu. Item radio. Button = new JRadio. Button. Menu. Item("Computer", true); menu. add(check. Box); menu. add(radio. Button); // Add two more Composites JMenu. Bar menu. Bar = new JMenu. Bar(); set. JMenu. Bar(menu. Bar); menu. Bar. add(menu); Run JMenu. Item. Demo. Composite. java See code demo page 10

Portfolios in Portfolios 11

Portfolios in Portfolios 11

Portfolio overall = new Portfolio("My Entire Retirement Portfolio"); // Add two leafs to "my

Portfolio overall = new Portfolio("My Entire Retirement Portfolio"); // Add two leafs to "my IRA" Stock a. Stock = new Stock("Seven Eleven", 500, 9. 15); overall. add(a. Stock); Bank. Account account = new Bank. Account("Swiss Account", 300000); overall. add(account); // Create a tech portfolio Portfolio tech = new Portfolio("Tech Stocks"); Stock sun = new Stock("Sun", 20, 30. 50); Mutual. Fund high. Risk = new Mutual. Fund("Nasdaq", 13, 45. 20); tech. add(sun); // add leaf tech. add(high. Risk); // add leaf // Add this 2 nd 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 pacific. Rim = new Stock("Pacific Rim Tech", 10, 12. 34); Mutual. Fund lrg. Grow = new Mutual. Fund("Large Growth", 100, 95. 21); global. add(pacific. Rim); global. add(lrg. Grow); tech. add(global); 12

Create a print method for this My Entire Retirement Portfolio with value $315417. 0

Create a print method for this My Entire Retirement Portfolio with value $315417. 0 500 shares of Seven Eleven with value $4575. 0 Bank. Account: '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