How to implement the java model design template for image objects: BufferedImage, Image, ImageIcon

advertisements

What would be an example of template method design pattern for image objects, or different types of image like: BufferedImage, Image, ImageIcon. I only need java class methods and their signatures and fields i don't need real functional code. this would be based on the fact that different types of image have same mechanisms for displaying titles but different mechanisms for displaying images.


In software engineering, the template method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in a method, called template method, which defers some steps to subclasses. It lets one redefine certain steps of an algorithm without changing the algorithm's structure.

First you need to declare an abstract class, which will be inherited by other image classes. It declares one abstract method meant to be implemented by other classes and one method that will be inherited by classes that implement this abstract class:

public abstract class Template{
    public String imageTitle;
    abstract public void displayImage(JLabel label); //abstract unimplemented method
    public void displayTitle(String imageTitle)
    {
        //Implement your display title method for all classes
    }
}

Then you can define your classes to implement the abstract method:

public class ImageIconClass extends Template{
    @Override
    public void displayImage(JLabel label) {
        // Implement specific display image method for ImageIconClass
    }
}

public class ImageClass extends Template{
    @Override
    public void displayImage(JLabel label) {
         // Implement specific display image method for ImageClass
    }
}

public class BufferedImageClass extends Template{
     @Override
    public void displayImage(JLabel label) {
       // Implement specific display image method for BufferedImageClass
    }
}

Jlabel is used to display the image.