Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 3-1 Composite Design Pattern Rick Mercer

2 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 3 General Form of Composite

4 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 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

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

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

8 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 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

10 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

11 Can add to any Container

12 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);

13 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);

14 Portfolios in Portfolios

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

16 Recursive toString result 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 with value $9521.0

17 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


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

Similar presentations


Ads by Google