the iterator pattern€¦ · • the iterator design pattern enables us to access the elements of...

21
Supplement to The Design and Implementation of Multimedia Software The Iterator Pattern Prof. David Bernstein James Madison University users.cs.jmu.edu/bernstdh D. Bernstein (JMU) Iterator www.cs.jmu.edu 1 / 21

Upload: others

Post on 15-Jul-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Supplement toThe Design and Implementation of Multimedia Software

The Iterator Pattern

Prof. David Bernstein

James Madison University

users.cs.jmu.edu/bernstdh

D. Bernstein (JMU) Iterator www.cs.jmu.edu 1 / 21

Page 2: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Motivation

• Computers vs. Calculators:Computers can perform the same operation many times

• Programming Languages:Harness this power using loops

• A Problem:Traditional looping requires an understanding of the structure ofthe ”aggregate”

D. Bernstein (JMU) Iterator www.cs.jmu.edu 2 / 21

Page 3: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Looping Over an Array

String city;

for (int i=0; i < cities.length; i++)

{

city = (String)cities[i];

System.out.println(city);

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 3 / 21

Page 4: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Looping Over an ArrayList

String city;

for (int i=0; i < cities.size(); i++)

{

city = (String)cities.get(i);

System.out.println(city);

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 4 / 21

Page 5: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Looping Over a Linked Structure

Node current;

String city;

current = first;

while (current != null)

{

city = (String)current.value;

System.out.println(city);

current = current.next;

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 5 / 21

Page 6: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Some Observations

• Applications often “loop” over the same aggregate object in manyclasses and methods

• Changing from one aggregate to another is, as a result, veryinconvenient

• The Iterator design pattern enables us to access the elements of anaggregate object while hiding its internal structure

D. Bernstein (JMU) Iterator www.cs.jmu.edu 6 / 21

Page 7: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Important Operations

• Reset its “pointer” (or cursor) to the first element

• Determine if there are any more elements in the sequence

• Move its “pointer” to the next element

• Retrieve the “current” element

D. Bernstein (JMU) Iterator www.cs.jmu.edu 7 / 21

Page 8: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

The Iterator Pattern

D. Bernstein (JMU) Iterator www.cs.jmu.edu 8 / 21

Page 9: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Uses in Java

• “Old” Aggregates:Aggregates: Vector, Hashtable

Iterator: Enumeration

• “New” Aggregates:Aggregates: ArrayList, HashSet

Iterator: Iterator

D. Bernstein (JMU) Iterator www.cs.jmu.edu 9 / 21

Page 10: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - Managing Names with a Vector

import java.io.*;

import java.util.*;

public class NameList

{

private Vector<String> names;

public NameList()

{

names = new Vector<String>();

}

public Enumeration<String> elements()

{

return names.elements();

}

public void read(String fn)

{

BufferedReader in;

String line;

try

D. Bernstein (JMU) Iterator www.cs.jmu.edu 10 / 21

Page 11: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - Managing Names with a Vector (cont.)

{

in = new BufferedReader(

new FileReader(fn));

while ( (line = in.readLine()) != null)

{

names.add(line);

}

in.close();

}

catch (IOException ioe)

{

System.err.println("Problem opening file: "+fn);

System.exit(1);

}

}

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 11 / 21

Page 12: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - Managing Names with a Hashtable

import java.io.*;

import java.util.*;

public class NameDatabase

{

private Hashtable<String, String> names;

public NameDatabase()

{

names = new Hashtable<String, String>();

}

public void add(String name)

{

names.put(name, name);

}

public Enumeration<String> elements()

{

return names.elements();

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 12 / 21

Page 13: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - Managing Names with a Hashtable (cont.)

public void read(String fn)

{

BufferedReader in;

String line;

try

{

in = new BufferedReader(

new FileReader(fn));

while ( (line = in.readLine()) != null)

{

add(line);

}

in.close();

}

catch (IOException ioe)

{

System.err.println("Problem opening file: "+fn);

System.exit(1);

}

}

public void remove(String name)

{

names.remove(name);

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 13 / 21

Page 14: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - Managing Names with a Hashtable (cont.)

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 14 / 21

Page 15: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - Using the NameList

// To use a NameList

//

NameList names;

names = new NameList();

D. Bernstein (JMU) Iterator www.cs.jmu.edu 15 / 21

Page 16: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - Using the NameDatabase

// To use a NameDatabase

//

NameDatabase names;

names = new NameDatabase();

D. Bernstein (JMU) Iterator www.cs.jmu.edu 16 / 21

Page 17: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - Using Either

// Nothing else has to change

//

Enumeration<String> iterator;

String name;

names.read("people.txt");

iterator = names.elements();

while (iterator.hasMoreElements())

{

name = iterator.nextElement();

System.out.println(name);

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 17 / 21

Page 18: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Other Benefits of the Iterator Pattern

• Several objects can be “looping” over the elements in theaggregate at the same time

• A “filtered” list (e.g., names starting with the letter “A”) ishandled in eactly the same way that the “unfiltered” version is

D. Bernstein (JMU) Iterator www.cs.jmu.edu 18 / 21

Page 19: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - One Iterator used by Many Objects

import java.util.*;

public class NamePrinter implements Runnable

{

private static int instances = 0;

private Enumeration iterator;

private int id;

private Thread controlThread;

public NamePrinter(Enumeration names)

{

iterator = names;

instances++;

id = instances;

controlThread = new Thread(this);

controlThread.start();

}

public void run()

{

int delay;

Random random;

String name;

D. Bernstein (JMU) Iterator www.cs.jmu.edu 19 / 21

Page 20: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - One Iterator used by Many Objects (cont.)

random = new Random(id*System.currentTimeMillis());

while (iterator.hasMoreElements())

{

name = (String)iterator.nextElement();

System.out.println(id+": "+name);

try

{

delay = random.nextInt(100);

controlThread.sleep(delay);

}

catch (InterruptedException ie)

{

// Ignore

}

}

}

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 20 / 21

Page 21: The Iterator Pattern€¦ · • The Iterator design pattern enables us to access the elements of an aggregate object while hiding its internal structure D. Bernstein (JMU) Iterator

Example - One Iterator used by Many Objects (cont.)

import java.util.*;

public class Driver2

{

public static void main(String[] args)

{

Enumeration iterator;

NameList names;

NamePrinter np1, np2, np3;

names = new NameList();

names.read("people.txt");

iterator = names.elements();

np1 = new NamePrinter(iterator);

np2 = new NamePrinter(iterator);

np3 = new NamePrinter(iterator);

}

}

D. Bernstein (JMU) Iterator www.cs.jmu.edu 21 / 21