对于图像对象或不同类型的图像(如:BufferedImage
、Image
、ImageIcon
),template method design pattern
的示例是什么。我只需要java类方法及其签名和字段,而不需要真正的函数代码。这将基于不同类型的图像具有用于显示标题的相同机制但用于显示图像的不同机制的事实。
模板方法模式是一种行为设计模式,它定义了方法中算法的程序框架。
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
}
}
}
正如我在上面所说的,模板方法定义了子类应该实现的算法的框架。这是一个美丽的设计模式知道。
理论:
模板方法模式允许您在超类的方法中定义算法的骨架,即所谓的模板方法。
在此模板方法中,调用一个或多个抽象方法来完成算法的某些步骤。这种抽象方法有时被称为占位符方法。
由于这些步骤是抽象的,因此在超类中没有实现。相反,它们是由子类以不同的方式实现的,因此在特定的子类实例上调用继承的模板方法将使用它提供的占位符运行算法。
实施:
考虑使用泛型来实现更健壮的模板模式,以及将占位符方法定义为protected
,因为它们只能从模板方法中调用。
如果不想让子类覆盖template方法,请在抽象超类中将其声明为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's 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's 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()
方法来为其提供实现。以下是泛型的大帮助,因为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
在软件工程中,模板方法模式是一种行为设计模式,它定义了方法中算法的程序骨架,称为模板方法,它将一些步骤推迟到子类中。它允许在不改变算法结构的情况下重新定义算法的某些步骤。
首先,您需要声明一个抽象类,它将被其他图像类继承。它声明了一个要由其他类实现的抽象方法以及一种将由实现这个抽象类的类继承的方法:
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
}
}
然后你可以定义你的类来实现抽象方法:
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用于显示图像。