Java Reference
In-Depth Information
import java.awt.*;
import javax.swing.*;
public class ImageIconLoader extends ImageIcon
implements Runnable
{
static final ImageIcon ABSENT =
new ImageIcon("absent.jpg");
static final ImageIcon LOADING =
new ImageIcon("loading.jpg");
protected String filename;
protected JFrame callbackFrame;
public ImageIconLoader(String filename)
{
super(ABSENT.getImage());
this.filename = filename;
}
public void load(JFrame callbackFrame)
{
// challenge!
}
public void run()
{
// challenge!
}
}
CHALLENGE 11.2
Fill in the code for load() and run() in ImageIconLoader .
The revised code is less coupled to the design of ImageIcon , relying primarily on
getImage() and setImage() rather than on the mechanics of which methods to forward.
In fact, no forwarding exists: ImageIconLoader is a proxy in spirit but not in structure.
Remote Proxies
The idea of a proxy is that one object intelligently forwards calls to another object. This can
become complex and fragile, as in the case of ImageIcon , where it is difficult to know
which calls to forward. Even if you forward every call, your code may break if the class of the
proxied object changes. When you encounter the P ROXY pattern in a design, you should
question whether the benefit justifies the accompanying fragility of code. There are cases in
which applying P ROXY is well justified, as when an object and its proxy are active on
different computers.
If the object whose method you want to call is running on another computer, you must find
a way to communicate with the remote object other than calling it directly. You could open
a socket on the remote machine and devise a protocol to pass messages to the remote object.
Ideally, such a scheme would let you pass messages in almost the same way as if the object
Search WWH ::




Custom Search