Two Design Patterns: Adapter and Composite - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Two Design Patterns: Adapter and Composite

Description:

Adapted ArrayList to store Songs and CDs ... s1.updatePrice(31.50); // up 50 cents // Show all elements with indents. overall.listAll ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 18
Provided by: rickmercer
Category:

less

Transcript and Presenter's Notes

Title: Two Design Patterns: Adapter and Composite


1
Two Design PatternsAdapter and Composite
  • C Sc 335

2
Adapter
  • The intent of Adapter is to provide the interface
    a client expects, using the services of a class
    with a different interface
  • We have used Adapter (also known as Wrapper)
  • Adapted HashMap to Jukebox to allow a small set
    of methods particular to the Jukebox
  • Adapted ArrayList to store Songs and CDs
  • Sometime the Adapter extends a class or
    implements an interface used in the client code

3
Object Adapter
  • JTable needs an instance of TableModel that
    represents data
  • It must have methods like
  • getColumnCount, getRowCount, getValueAt
  • You can adapt any of your data to the interface
    expected by JTable by implementing all methods

4
Show Table of Employees
  • public static void main(String args)
  • WeeklyEmployee w1
  • new WeeklyEmployee("Devon", 40.0, 15.75, 3,
    "M")
  • WeeklyEmployee w2
  • new WeeklyEmployee("Kim", 0.0, 12.50, 1,
    "S")
  • WeeklyEmployee w3
  • new WeeklyEmployee("Chris", 35.0, 20.50, 2,
    "M")
  • WeeklyEmployee employees w1, w2, w3
  • EmployeeTable empTable
  • new EmployeeTable(employees)
  • EmployeeTableFrame window
  • new EmployeeTableFrame(empTable)
  • window.show()

5
The View
  • class EmployeeTableFrame extends JFrame
  • private JTable view
  • public EmployeeTableFrame(EmployeeTable
    empTable)
  • setTitle("Adapter")
  • setSize(300, 200)
  • view new JTable(empTable)
  • getContentPane().add(view)
  • pack()

6
EmployeeTable class
  • class EmployeeTable implements TableModel
  • private int rows, cols
  • private WeeklyEmployee data
  • public EmployeeTable(WeeklyEmployee
    employeeData)
  • data employeeData
  • cols 4
  • rows data.length

7
Team Activity
  • public int getColumnCount()
  • // What goes here?
  • public int getRowCount()
  • // What goes here?

8
JTable Populates Cells
  • public Object getValueAt(int row, int col)
  • if (col 0)
  • return datarow.getName()
  • else if (col 1)
  • return "" datarow.grossPay()
  • else if (col 2)
  • return "" datarow.getTotalTaxes()
  • else if (col 3)
  • return "" (datarow.grossPay() -
  • datarow.getTotalTaxes())
  • else return null

9
Needs formatting
10
Extend or Implement?
  • Still need 7 more methods if implementing
    TableModel, in which case, you need
  • public Class getColumnClass(int col)
  • return getValueAt(0, col).getClass()
  • Or could extend AbstractTableModel
  • then override the methods

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

12
General Form
13
Client
  • Portfolio overall new Portfolio("my IRA")
  • // Add one leaf
  • overall.add(new Stock("Seven Eleven", 500,
    9.15))
  • // Build and add a Portfolio to a Portfolio
  • Portfolio tech new Portfolio("Tech stocks")
  • Stock s1 new Stock("Sun", 20, 30.50)
  • tech.add(s1)
  • tech.add(new MutualFund("Microsoft", 13, 45.20))
  • overall.add(tech)
  • // Can update prices
  • s1.updatePrice(31.50) // up 50 cents
  • // Show all elements with indents
  • overall.listAll()

14
Desired Output
  • my IRA 5762.6
  • 500 shares Seven Eleven 4575.0
  • Tech stocks 1187.6
  • 20 shares Sun 600.0
  • 13.0 shares Microsoft 587.6

15
Team Activity
  • A) Draw the UML Diagram to capture the real
    world example of stocks, mutual funds, and
    portfolios
  • List all class names
  • Draw the relationships
  • Place the listAll method in the correct place
  • Place the add method in the correct place
  • Place the getValue methods in the correct place

16
An Answer
17
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
  • B) Using the UML diagram, write the Java code for
    Asset and Commodity (a leaf)

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

19
Answer
  • 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

20
Stock and MutualFund
  • C) Implement Mutual Fund and Stock
  • D) Implement Portfolio with the getValue method
    only (no listAll)

21
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)

22
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
Write a Comment
User Comments (0)
About PowerShow.com