User Tools

Site Tools


blog:duck_typing_helps_automatically_dispose_swt_images

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
blog:duck_typing_helps_automatically_dispose_swt_images [2008/04/26 23:28]
djo
blog:duck_typing_helps_automatically_dispose_swt_images [2014/10/17 22:08] (current)
Line 1: Line 1:
 +====== Duck Typing helps automatically dispose SWT Images ======
 +
 +Let's look at a really practical example of why Duck Typing is important in Java. This example will use Java reflection to hack duck typing but will provide a basis for future discussion of how to really implement the technique in Java.
 +
 +If you need to make SWT automatically dispose Image objects, here's a simple class that gets the job done quite painlessly:
 +
 +<code java>
 +    public class ImageMgr implements DisposeListener {
 +
 +        private Image image;
 +
 +        public ImageMgr(Image image) {
 +            this.image = image;
 +        }
 +
 +        public ImageMgr(Control receiver, Image image) {
 +            this.image = image;
 +            try {
 +                // Use duck typing--any class with setImage() will work
 +                Method setImage = receiver.getClass().getDeclaredMethod("​setImage",​
 +                                       new Class[] {Image.class});​
 +                setImage.invoke(receiver,​ new Object[] {image});
 +            } catch (Exception e) {
 +                // Programmer error: throw a RuntimeException
 +                Logger.log().error(e,​ "​Unable to setImage()"​);​
 +                throw new RuntimeException(e);​
 +            }
 +            receiver.addDisposeListener(this);​
 +        }
 +
 +        public void widgetDisposed(DisposeEvent e) {
 +            if (!image.isDisposed())
 +                image.dispose();​
 +        }
 +
 +        public Image get() {
 +            return image;
 +        }
 +
 +    }
 +</​code>​
 +
 +Notice that if the control you're putting the Image on uses the setImage() method name (most do), you can just use the convenience constructor as follows:
 +
 +<code java>
 +    CLabel label = new CLabel(parent,​ SWT.NULL); ​ // Class with setImage() method
 +    new ImageMgr(label,​ new Image(Display.getCurrent(),​
 +                 ​getClass().getResourceAsStream("​icons/​etool16/​image.gif"​)));​
 +</​code>​
 +
 +This will automatically add the image to the control with the setImage() method (we're using a CLabel as an example here) and automatically set the ImageMgr object as a dispose listener on the control. Now when the SWT control is disposed (automatically by its parent), the Image will automatically be disposed too.
 +
 +But what does all of this have to do with duck typing?
 +
 +===== Naming Conventions create Duck Types =====
 +
 +In Java, a Duck Type is an interface that is implemented consistently throughout a framework but is never declared anywhere.
 +
 +For example, nearly every SWT class that accepts an Image does so via a setImage() method call. However, there is no SettableImage interface anywhere in SWT that is implemented by all of these classes. Furthermore,​ there are so many of these naming conventions,​ or Duck Interfaces, in SWT that it would be absurd for SWT to actually declare all of these interfaces and implement them consistently everywhere. Nearly every SWT class would be implementing a ton of interfaces!
 +
 +However, this class illustrates the convenience of being able to use Duck Typing in Java to be able to treat all classes with a similar behavior the same way.
 +
 +In a future article, we will look at how to get our interfaces back and fully implement Duck Typing and Duck Interfaces in Java.
 +
 +~~LINKBACK~~
 +~~DISCUSSION:​closed~~
  
blog/duck_typing_helps_automatically_dispose_swt_images.txt ยท Last modified: 2014/10/17 22:08 (external edit)