Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 根据屏幕分辨率加载图像的设计模式_Java_Design Patterns - Fatal编程技术网

Java 根据屏幕分辨率加载图像的设计模式

Java 根据屏幕分辨率加载图像的设计模式,java,design-patterns,Java,Design Patterns,我正在根据屏幕分辨率向屏幕添加图像。因此,根据屏幕分辨率,我添加适当大小的图像。这是否属于任何设计模式?或者是否有一种设计模式更适合此要求 ImageFactory.getBitmap(); public static Bitmap getBitmap(){ if(screenWidth == 360 && screenHeight== 480){ return new Bitmap("360480.bmp");

我正在根据屏幕分辨率向屏幕添加图像。因此,根据屏幕分辨率,我添加适当大小的图像。这是否属于任何设计模式?或者是否有一种设计模式更适合此要求

ImageFactory.getBitmap();

    public static Bitmap getBitmap(){   

        if(screenWidth == 360 && screenHeight== 480){
            return new Bitmap("360480.bmp");
        }
        else {
            return new Bitmap("320240.bmp");
        }   
    }

这看起来像一个工厂模式。通过考虑屏幕大小,您的工厂在创建/返回位图方面非常聪明,从我看到的情况来看,我认为您在这方面做得不错


当你在做这件事的时候,你可能想考虑一下图像的命名模式(我认为像“640x480.bmp”这样的东西比“640480.bmp”更容易看。)

看起来,在这种特殊情况下,你可以不使用约定策略(而不是设计模式)

大概是这样的:

public static Bitmap getBitmap(){ 
    String fileName = Integer.toString(screenWidth)
        + Integer.toString(screenHeight) + ".bmp"

    File f = new File(fileName);  

    if(f.exists()){
        return new Bitmap(fileName);
    }
    else {
        return new Bitmap("320240.bmp");
    }   
}