Composite Design Pattern - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Composite Design Pattern

Description:

See demo page. 11. Another Composite ... tech.add(sun); // add leaf. tech.add(highRisk); // add leaf ... List base class as Asset (Component from 3) ... – PowerPoint PPT presentation

Number of Views:385
Avg rating:3.0/5.0
Slides: 14
Provided by: rickmercer
Category:

less

Transcript and Presenter's Notes

Title: Composite Design Pattern


1
Composite Design Pattern
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 interfaces for accessing and managing
    its child components.
  • Leaf Has no children it is a primitive
  • Composite Defines behavior for components having
    children. Implements child-related operations of
    the Component interface

5
Participants
  • Component has operations, which are methods that
    apply to all components
  • The component can be a Composite or a Leaf
  • Component also has composite methods add( ),
    remove( ), and getChild( )
  • In each methods, a Component is passed
  • Component itself could throw an exception for
    these methods
  • you can not add a Component to a leaf

6
Participants
  • Leaf implements the operations
  • Composite implements composite methods
  • Composite also implements the leaf operations by
    invoking the operation on its children

7
Use Example Java Swing
  • Java Swing has four major pieces
  • Components
  • Events and EventListeners
  • Layouts
  • Graphics
  • Component utilizes the Composite pattern in
    several ways. As such, it is a useful exemplary
    of this pattern

8
Menus in Java Swing
  • Java menus use the Composite pattern
  • JComponent is a Component (abstract)
  • JMenuBar and JMenuItem are Composites that
    extends JComponent
  • JMenuItem has three subclasses
  • JMenu
  • JRadioButtonMenuItem
  • JCheckboxMenuItem
  • Leafs JLabel, JTextField

9
  • JMenuItem menu new JMenu("Composite")
  • menu.setMnemonic('C')
  • JMenuItem jmi1 new JMenu("JMenu")
  • JMenuItem jmiNested1 new JMenu("Nested 1")
  • JMenuItem jmiNested2 new JMenu("Nested 2")
  • jmi1.add(jmiNested1)
  • jmi1.add(jmiNested2)
  • JLabel label new JLabel("Label")
  • JTextField textF new JTextField("text
    field")
  • jmi1.add(label)
  • jmi1.add(textF)
  • // Leafs (promitives) ignored add, nothing
    happens
  • label.add(new JLabel("no"))
  • textF.add(new JLabel("go"))

10
  • JMenuItem jmi2 new JCheckBoxMenuItem("Human",
    false)
  • JMenuItem jmi3 new JRadioButtonMenuItem("Compute
    r", true)
  • menu.add(jmi2)
  • menu.add(jmi3)
  • menu.add(jmi1)
  • JMenuBar menuBar new JMenuBar()
  • setJMenuBar(menuBar)
  • menuBar.add(menu)
  • Run JMenuItemDemoComposite.java
  • See demo page

11
Another Composite
  • Portfolio overall new Portfolio("my IRA")
  • // Add two leafs to "my IRA"
  • overall.add(new Stock("Seven Eleven", 500,
    9.15))
  • overall.add(new BankAccount("Swiss Account",
    300000))
  • // 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
  • // Show all elements with indents (output next
    slide
  • overall.listAll()

12
Desired Output
  • my IRA 305772.6
  • 500 shares Seven Eleven 4575.0
  • Swiss Account 300000.0
  • Tech stocks 1197.6
  • 20 shares Sun 610.0
  • 13.0 shares Nasdaq 587.6

13
Team Activity
  • Let a UML Diagram capture the real world example
    of bank accounts stocks, mutual funds, and
    portfolios
  • List base class as Asset (Component from slide 3)
  • Let Commodity extend Asset (there are 2 commodity
    types)
  • Stock extends Commodity (a leaf)
  • MutualFund extends Commodity (a leaf)
  • Let BankAccount extend Asset (a leaf)
  • Let Portfolio extend Asset (Composite from slide
    3)
  • Can store Stocks, BankAccounts, MutualFunds
    (leafs) or other Portfolios (Composite) because
    they are all assets
  • Place these methods in the correct place
  • listAll add getValue

14
One Answer
Write a Comment
User Comments (0)
About PowerShow.com