Design Patterns Java™ Workbook

(Michael S) #1
Chapter 11. Proxy

public ImageIconProxy(String filename)
{
super(ABSENT.getImage());
this.filename = filename;
}


public void load(JFrame callbackFrame)
{
this.callbackFrame = callbackFrame;
current = LOADING;
callbackFrame.repaint();
new Thread(this).start();
}


public void run()
{
current = new ImageIcon(filename);
callbackFrame.pack();
}


public int getIconHeight()
{
// challenge!
}


public int getIconWidth()
{
// challenge!
}


public synchronized void paintIcon(
Component c, Graphics g, int x, int y)
{
// challenge!
}
}


CHALLENGE 11.1


An ImageIconProxy object accepts three image display calls that it must pass on
to the current image. Write the code for getIconHeight(), getIconWidth(),
and paintIcon() for the ImageIconProxy class.

Suppose that you get the code working for this small demonstration application. Before you
build the real application, which has more than just a Load button, you hold a design review,
and the fragility of your design comes to light.


As with many proxies, there is a dangerous dependency between ImageIcon and
ImageIconProxy. The current design assumes that you have correctly ascertained, or
guessed, the right methods to override. Even if you have, the ImageIcon class can change in
a future release and break your code. You and the design review team decide to rework your
approach.

Free download pdf