Composite Design Pattern Rick Mercer 3 Composite Pattern

















- Slides: 17
Composite Design Pattern Rick Mercer 3 -
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 2 Composite was used in the View class of
General Form of Composite 3
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 4 components having children such as add,
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 5
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 Array. List<Object> a = new Array. List<Object>(); a. add("abc"); a. add(123); Array. List<Object> b = new Array. List<Object>(); b. add(1. 11); What types are the b. add(true); System. out. println("a: " + a); Leafs? System. out. println("b: " + b); What type is the b. add(a); Composite? b. add(b); String, Integer, Double, // a. add(b); Stack Overflow What type is the Boolean Array. List System. out. println("a: " + a); Component? Object a: [abc, 123] b: [1. 11, true] a: [abc, 123] System. out. println("b: " + b); b: [1. 11, true, [abc, 123], Output? (this Collection)]`
Use Example: Java Swing has four major pieces: – Events and Event. Listeners – Layouts – Drawing – Graphical Components • The root of all of these is named: Component utilizes the Composite pattern in several ways, with menus for example 9 – One you may find useful or need for your
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
Can add to any Container
JMenu. Item menu = new JMenu("Composite"); // Add two items 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"); jmi 1 Nest. add(jmi. Nested 2); 12
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);
Portfolios in Portfolios
Portfolio overall = new Portfolio("My IRA"); // Add two leafs to "my IRA" Stock a. Stock = new Stock(”Star Lucks", 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 oracle = new Stock(”Oracle", 20, 30. 50); Mutual. Fund high. Risk = new Mutual. Fund("Nasdaq", 13, 45. 20); tech. add(oracle); // 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 an 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);
Recursive to. String result 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 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