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

What would be an example of template method design patternan image or for objects of different types of images, such as: BufferedImage, Image, ImageIcon. I only need the methods of the java class, their signatures and fields, I do not need real functional code. this would be based on the fact that different types of images have the same mechanisms for displaying names, but different mechanisms for displaying images.

+4
source share
3 answers

In software development, a template template template is a behavior template that defines the software skeleton of an algorithm in a method called a template method that discards some steps into subclasses. It allows you to redefine certain stages of the algorithm without changing the structure of the algorithm.

First you need to declare an abstract class that will be inherited by other image classes. It declares one abstract method intended for implementation 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.

0
source

, .

Smalltalk printString - .

, java draw (Graphics g), .

. , ,

public void draw(Image i);

   class Image {
        int x1,y1,x2,y2; // may be boundaries of image common to all inherited 
objects
        String title;

        protected void displayTitle() {

        }
        // some code goes here
        public void draw() {
             // some code common to all image drawing
             // this might involve x and y declared above
             displayTitle();
             drawImage();
             // some more code after the template method
        }
        // the subclasses inheriting define their own algorithm to display image
        public abstract void drawImage();
    }

    class BufferedImage extends Image {
        // defines its own algorithm    
        public void drawImage() {
            // BufferedImage specific code to display the image
        }
    }

    class IconImage extends Image {
        // defines its own algorithm
        public void drawImage() {
            // IconImage specific code to display the image
        }
    }

    class DriverProgram {
        public static void main(String[] args) {
            Image[] image = {new BufferedImage(),new IconImage(),new XYXImage()};
            for(Image img:image) {
                img.draw();  // calling the template method
            }
        }
    }

, , . .

+2

:

, .

. -.

, . -, .

:

, - protected, .

, , final .

, BufferedImage ImageIcon Image () :

// Class that defines the template method
// Generic parameter allows to define the specific type of image
// that will be handled by this image renderer
public abstract class ImageRenderer<T extends Image> {

    // This is the template method
    // It final to avoid that subclasses override it
    public final void display(String title, T image) {
        // Display title
        this.displayTitle(title);
        // Let subclasses display specific type of image
        this.displayImage(image);
    }

    // Display title for every image type
    // This method is private since it only called
    // from within the template method
    // (make it protected if you want to let subclasses 
    // override it, i.e. for custom title displaying)
    private void displayTitle(String title) {
        // Display title, no matter the image type
    }

    // Placeholder method, no implementation
    // Actual implementation is delegated to subclasses
    protected abstract void displayImage(T image);
}

BufferedImageRenderer displayImage(), . , generics , displayImage() :

public class BufferedImageRenderer 
    extends ImageRenderer<BufferedImage> {

    @Override
    protected void displayImage(BufferedImage image) {
        // Display specific buffered image
    }
}

ImageIconRenderer:

public class ImageIconRenderer 
    extends ImageRenderer<ImageIcon> {

    @Override
    protected void displayImage(ImageIcon image) {
        // Display specific image icon
    }
}

, , , .. ImageIcon:

ImageIcon icon = getImageIconFromSomePlace();
String iconTitle = "My pretty icon";

ImageIconRenderer renderer = new ImageIconRenderer();
renderer.displayImage(iconTitle, icon);

, displayImage() , , :

BufferedImage bufferedImage = getBufferedImageFromSomePlace();
String bufferedImageTitle = "My amazing buffered image";

ImageIconRenderer renderer = new ImageIconRenderer();
renderer.displayImage(bufferedImageTitle, bufferedImage); // compilation error
0
source

All Articles