Appendix B. Solutions
Iterator (Chapter 28)..................................................................................................................................
SOLUTION 28.1....................................................................................................................................
A working program is:
package com.oozinoz.applications;
import com.oozinoz.fireworks.*;
public class ShowFireworkList
{
public static void main(String[] args)
{
FireworkList flist =
Promo.getPromotionalFireworks();
FireworkList.Itr i = flist.iterator();
while (i.hasNext())
{
Firework f = i.next();
System.out.println(
f.getName()
- ", $" + f.getPrice()
- ", " + f.getType());
}
}
}
SOLUTION 28.2....................................................................................................................................
As Chapter 16, Factory Method, describes, iterators provide a classic example of
the FACTORY METHOD pattern. A client that wants an iterator for an instance of
a ProcessComponent knows when to create the iterator, but the receiving class knows
which class to instantiate.
SOLUTION 28.3....................................................................................................................................
You need to change the next() code to not return the node of a composite that you're
iterating over. You also need to pass along the value of the showInterior Boolean to
subiterators you create:
public Object next()
{
if (peek != null)
{
Object o = peek;
peek = null;
return o;
}
if (!visited.contains(node))
{
visited.add(node);