Design Patterns Java™ Workbook

(Michael S) #1
Appendix B. Solutions

Proxy (Chapter 11).....................................................................................................................................


SOLUTION 11.1....................................................................................................................................


The image-display methods of ImageIconProxy forward their calls to the current image:


public int getIconHeight()
{
return current.getIconHeight();
}


public int getIconWidth()
{
return current.getIconWidth();
}


public synchronized void paintIcon(
Component c, Graphics g, int x, int y)
{
current.paintIcon(c, g, x, y);
}


SOLUTION 11.2....................................................................................................................................


The load() method sets the image to Loading..., whereas the run() method, executing in a
separate thread, loads the desired image:


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


public void run()
{
setImage(new ImageIcon(filename).getImage());
callbackFrame.pack();
}


SOLUTION 11.3....................................................................................................................................


As the class diagram shows, a RocketImpl constructor accepts a price and an apogee:


Rocket biggie = new RocketImpl(29.95, 820);


You could declare biggie to be of type RocketImpl. However, what is important about
biggie is that it fulfills the Rocket interface that a client will look for.

Free download pdf