Adapter Design Pattern - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Adapter Design Pattern

Description:

In a similar fashion, JTable needs an instance of TableModel that represents the ... title='Sample Slide Show' date='Date of publication' author='Yours Truly' ... – PowerPoint PPT presentation

Number of Views:110
Avg rating:3.0/5.0
Slides: 13
Provided by: rickmercer
Category:

less

Transcript and Presenter's Notes

Title: Adapter Design Pattern


1
Adapter Design Pattern
  • C Sc 335
  • Rick Mercer

2
Adapter
  • The intent of Adapter is to provide the interface
    a client expects, using a class with a different
    interface (methods)
  • Sometimes called a wrapper because an adapter
    class wraps the implementation of another class
    in the desired interface

3
In Java 1.4
  • Before Java 5.0, we often adapted an ArrayList or
    HashMap to have an easier to use collection see
    wiki
  • Use a Containment Relationship
  • A collection class with an ArrayList or HashMap
    instance variable
  • Put the cast in the method once instead of
    throughout code
  • http//www.refactoring.com/catalog/encapsulateDown
    cast.html
  • Add Employees rather than Objects (type safe)
  • Method names mean more to the client
  • getEmployeeWithID rather than get
  • Not as compelling for use with Java 5
  • Generics now provides type safety
  • No need to cast

4
Object Adapter
  • Adapters also come about by extending a class or
    implementing an interface used by the client code
  • You have used Adapter in this manner
  • Adapted your song collection so it could be
    stored in a ListModel object, which in turn was
    used by a JList to show a graphical view of the
    list elements

5
JTable is like JList
  • In a similar fashion, JTable needs an instance of
    TableModel that represents the model (data)
  • JTable Graphical view of data, rows/columns
  • Your new class must have methods such as
  • getColumnCount, getRowCount, getValueAt
  • Why? Because JTable uses them to populate view
  • You adapt your data to the interface expected by
    JTable by implementing all 10 methods

6
EmployeeTable class
  • // Store an array of Employee objects that
  • // can be put into a table
  • import javax.swing.event.TableModelListener
  • import javax.swing.table.TableModel
  • public class EmployeeList implements TableModel
  • private int rows 3
  • private int cols 4
  • private Employee data new Employeerows
  • public EmployeeList()
  • data0 new Employee("Devon",40, 15.75, 3,
    "M")
  • data1 new Employee("Kim", 0, 12.50, 1,
    "S")
  • data2 new Employee("Chris", 35, 20.50,
    2,"M")
  • // continued

7
JTable Populates Cells
  • // Part of the TableModel interface
  • public Object getValueAt(int row, int col)
  • switch (col)
  • case 0
  • return datarow.getName()
  • case 1
  • return datarow.grossPay()
  • case 2
  • return datarow.incomeTax()
  • case 3
  • return datarow.grossPay()-datarow.incomeTa
    x()
  • default
  • return null

8
Implement 2 More Methods
  • public int getColumnCount() // TableModel
    interface
  • // What goes here?
  • public int getRowCount() // TableModel
    interface
  • // What goes here?

9
The View
  • class EmployeeFrame extends JFrame
  • public static void main(String args)
  • new EmployeeFrame().setVisible(true)
  • public EmployeeFrame()
  • EmployeeList threeEmps new EmployeeList()
  • setTitle("Adapter")
  • setSize(300, 120)
  • setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  • JTable view new JTable(threeEmps)
  • JScrollPane scroller new JScrollPane(view)
  • this.add(scroller, BorderLayout.CENTER)

10
A JModel in a JTable
  • This could also be done by extending class
    AbstractTableMode
  • See MouseAdapter and WindowAdapter

11
JTree and TreeModel
  • An adapter could also convert the interface of a
    Document Object Model of an XML document into a
    tree structure that can be displayed

lt?xml version'1.0' encoding'utf-8'?gt ltslideshow
title"Sample Slide Show" date"Date of
publication" author"Yours Truly"gt ltslide
type"all"gt lttitlegtWake up to
WonderWidgets!lt/titlegt lt/slidegt ltslide
type"all"gt lttitlegtOverviewlt/titlegt
ltitemgtWhy ltemgtWonderWidgetslt/emgt are
greatlt/itemgt ltitem/gt ltitemgtWho
ltemgtbuyslt/emgt WonderWidgetslt/itemgt
lt/slidegt lt/slideshowgt
12
MouseAdapter
  • Swing include Adapters to provide an object with
    all the methods needed to implement an interface
  • Demo MouseAdapter
  • A class that implements all methods expected by a
    MouseListener and MouseMotionListener
  • Convenience? You override what few methods you
    need
  • WindowAdapter ...
Write a Comment
User Comments (0)
About PowerShow.com