Composite Design Pattern - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Composite Design Pattern

Description:

Composite Design Pattern – PowerPoint PPT presentation

Number of Views:133
Avg rating:3.0/5.0
Slides: 10
Provided by: rickmercer
Category:

less

Transcript and Presenter's Notes

Title: Composite Design Pattern


1
Composite Design Pattern
  • Rick Mercer
  • C Sc 335

2
Composite Design Pattern
  • Compose objects into tree structures to represent
    part-whole hierarchies.
  • Composite lets clients treat individual objects
    and compositions of objects uniformly

3
General Form
4
Client
  • 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()

5
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

6
Team Activity
  • A) 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

7
One Answer
8
Implement Asset Commodity
  • While the Value of a Portfolio is the sum of all
    assets, the value of a Commodity is the shares
    times the price
  • Using the UML diagram, write tests and Java code
    for
  • Asset
  • BankAccount
  • Commodity
  • Stock
  • MutualFund

9
Asset
  • abstract class Asset
  • private String myName
  • public Asset(String name)
  • myName name
  • public String toString()
  • return myName " " getValue()
  • public abstract double getValue()

10
BankAccount
  • class BankAccount extends Asset
  • private double balance
  • public BankAccount(String name, double
    initialBalance)
  • super(name)
  • balance initialBalance
  • public double getValue()
  • return balance

11
Commodity
  • abstract class Commodity extends Asset
  • private double myUnits
  • private double myPrice
  • public Commodity(String name, double shares,
    double price)
  • super(name)
  • myUnits shares
  • myPrice price
  • public double getValue()
  • return myUnits myPrice

12
Answer
  • class Stock extends Commodity
  • public Stock(String company, int shares, double
    price)
  • super(shares " shares " company, price,
    shares)
  • class MutualFund extends Commodity
  • public MutualFund(String fund, double shrs,
    double price)
  • super(shares " shares " fund, shrs,
    price)

13
Stock and MutualFund
  • D) Implement Portfolio with the getValue method
    only (no listAll)

14
Answer
  • class Portfolio extends Asset
  • private ArrayList myAssets
  • public static final String INDENT " "
  • public Portfolio(String name)
  • super(name)
  • myAssets new ArrayList()
  • public void add(Asset h)
  • myAssets.add(h)
  • public double getValue()
  • double sum 0.0
  • Iterator itr myAssets.iterator()
  • while (itr.hasNext())
  • sum ((Asset) itr.next()).getValue()
  • return sum
Write a Comment
User Comments (0)
About PowerShow.com