Two Design Patterns: Iterator and Adapter - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Two Design Patterns: Iterator and Adapter

Description:

Map String, String h = new TreeMap String, String (); h.put('one', '1 value' ... Can iterate over collections that have different data structures to store elements ... – PowerPoint PPT presentation

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

less

Transcript and Presenter's Notes

Title: Two Design Patterns: Iterator and Adapter


1
Two Design PatternsIterator and Adapter
  • C Sc 335
  • Rick Mercer

2
Iterator
  • Name Iterator (a.k.a Enumeration)
  • Problem How can you traverse over all objects in
    any collection. You dont want to change client
    code when the collection changes. You also want
    the same interface (methods)
  • Solutions
  • Design an interface
  • Have each class implement an interface.

3
Java 1.4 version of Iterator
  • public interface Iterator
  • // Return true if the iteration has at least
  • // one more elements to visit
  • boolean hasNext()
  • // Returns the next element in the iteration
  • Object next()
  • // Removes from the underlying collection the
    last
  • // element returned by the iterator (optional)
    void remove()

4
Java 5 version of Iterator
  • public interface IteratorltEgt
  • // Return true if the iteration has at least
  • // one more elements to visit
  • boolean hasNext()
  • // Returns the next element in the iteration
  • E next()
  • // Removes from the underlying collection the
    last
  • // element returned by the iterator (optional)
    void remove()

5
Java 1.4 Code
  • Map h new TreeMap()
  • h.put("one", "1 value")
  • h.put("two", "vaLue 2!")
  • h.put("three", "Value Three")
  • h.put("four", "Value_4th")
  • Collection c h.values()
  • Iterator itr c.iterator()
  • String str null
  • while (itr.hasNext())
  • str (String) itr.next() // Cast required
  • System.out.println(str.toUpperCase())
  • Set set h.keySet()
  • System.out.println(set)

VALUE_4TH 1 VALUE VALUE THREE VALUE 2! four,
one, three, two
6
Java 1.4 Code
  • MapltString, Stringgt h new TreeMapltString,
    Stringgt()
  • h.put("one", "1 value")
  • h.put("two", "vaLue 2!")
  • h.put("three", "Value Three")
  • h.put("four", "Value_4th")
  • CollectionltStringgt c h.values()
  • IteratorltStringgt itr c.iterator()
  • String str null
  • while (itr.hasNext())
  • // no cast, Java does the cast for us unboxs
  • System.out.println(itr.next().toUpperCase())
  • Set set h.keySet()
  • System.out.println(set)

VALUE_4TH 1 VALUE VALUE THREE VALUE 2! four,
one, three, two
7
Benefits
  • Consequences
  • Can change collection class without changing code
    to traverse the collection
  • Can provide a well-known interface to other
    objects
  • Can iterate over collections that have different
    data structures to store elements
  • Let the iterator do the work

8
UML Diagram of Java 1.4's Iterator and List
interface
ltltinterfacegtgt List iterator()
ltltinterfacegtgt Iterator hasNext() next()
Vector iterator()
LinkedList iterator()
ArrayList iterator()
Iterator hasNext() next()
Context
9
Adapter
  • The intent of Adapter is to provide the interface
    a client expects, using the services of a class
    with a different interface
  • You may have used Adapter (also known as Wrapper)
  • Adapted HashMap to Jukebox to allow only 3
    methods particular to the Jukebox
  • Provides type safety can only add Song objects
  • Avoids a lot of casts have the get method return
    Song objects, not Object objects

10
  • Adapted ArrayList to store Songs
  • Provides type safety can only add Song objects
  • Avoids a lot of casts have the get method return
    Song objects, not Object objects
  • Not as compelling a usage in Java 5
  • generics provides type safety
  • remove the need to cast

11
Object Adapter
  • Sometime the Adapter extends a class or
    implements an interface used in the client code
  • JTable needs an instance of TableModel that
    represents the model (data)
  • 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

12
Show Table of Employees
  • Employee w1
  • new Employee("Devon", 40, 15.75, 3, "M")
  • Employee w2
  • new Employee("Kim", 0, 12.50, 1, "S")
  • Employee w3
  • new Employee("Chris", 35, 20.50, 2, "M")
  • Employee employees w1, w2, w3
  • EmployeeTable empTable
  • new EmployeeTable(employees)

13
EmployeeTable class
  • // Store an array of Employee objects that
  • // can be put into a table
  • class EmployeeTable implements TableModel
  • private int rows, cols
  • private Employee data
  • public EmployeeTable(Employee employeeData)
  • data employeeData
  • cols 4
  • rows data.length
  • // continued

14
Team Activity
  • public int getColumnCount() // TableModel
    interface
  • // What goes here?
  • public int getRowCount() // TableModel
    interface
  • // What goes here?

15
JTable Populates Cells
  • // TableModel interface
  • 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

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

17
Needs formatting
// Tell the JFrame what to show EmployeeFrame
window new EmployeeFrame(empTable) window.setVi
sible(true)
Write a Comment
User Comments (0)
About PowerShow.com