英文:
Designing resource access: Final enums vs static members of extendable classes
问题
我正在尝试设计一个用于加载资源的框架。所以我有两个选项:
-
方法 1
底层结构: 一个带有简单的抽象类
实现和更具体的扩展
的接口
。
访问方式: 类的静态常量成员初始化为该类。 -
方法 2
底层结构: 从一开始就是一个特定的枚举
,实现了所有接口方法:没有利用抽象类的部分实现。
访问方式: 作为枚举条目
尽管方法 1 更加灵活和可重用,但我喜欢枚举提供的干净可用的列表方式,可以直接使用,而不是具有Class.instance.instance.instance. ...
范围的static final
。
是否有一种标准的方法来做到这一点?是否有更好的方法来做到这一点?
虽然在这里并不是绝对必要的,这是代码的一部分:
方法 1
接口 LoadableResource<T>
import java.util.ArrayDeque;
import java.util.Queue;
public interface LoadableResource<T> {
Queue<Exception> exceptionQueue = new ArrayDeque<>();
boolean load();
boolean isLoaded();
T getResource();
void onLoadFail();
void onLoadSuccess();
void onException(Exception ex);
Queue<Exception> getExcpetions();
}
抽象类 SimpleLoadableResource<T>
import java.util.Queue;
public abstract class SimpleLoadableResource<T> implements LoadableResource<T> {
private boolean FLAG_LOADED = false;
private T resource;
@Override
public boolean isLoaded() {
return FLAG_LOADED;
}
@Override
public T getResource() {
return resource;
}
@Override
public void onLoadFail() {}
@Override
public void onLoadSuccess() {}
@Override
public void onException(Exception ex) { exceptionQueue.add(ex); }
@Override
public Queue<Exception> getExcpetions() { return exceptionQueue; }
protected void setLoaded(boolean FLAG_LOADED) {
this.FLAG_LOADED = FLAG_LOADED;
}
public abstract T loader() throws Exception;
@Override
public boolean load() {
try {
resource = loader();
} catch (Exception e) {
onException(e);
}
if (isLoaded())
onLoadSuccess();
else
onLoadFail();
return isLoaded();
}
}
具体类 SimpleLoadableImage
import javax.imageio.ImageIO;
import java.awt.*;
public class SimpleLoadableImage extends SimpleLoadableResource<Image> {
public static final SimpleLoadableImage LOGO1 = new SimpleLoadableImage("1.jpg");
public static final SimpleLoadableImage LOGO2 = new SimpleLoadableImage("2.jpg");
private final String path;
public SimpleLoadableImage(String path) {
this.path = path;
super.load();
}
@Override
public Image loader() throws Exception {
var res = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(path));
setLoaded(true);
return res;
}
}
方法 2
import javax.imageio.ImageIO;
import java.awt.*;
import java.util.ArrayDeque;
import java.util.Queue;
public enum SimpleLoadableImage_Enum {
LOGO1("1.jpg"),
LOGO2("2.jpg");
private final String path;
Queue<Exception> exceptionQueue = new ArrayDeque<>();
private boolean FLAG_LOADED = false;
private Image resource;
private SimpleLoadableImage_Enum(String path) {
this.path = path;
try {
resource = ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(path));
FLAG_LOADED = true;
} catch (Exception e) {
exceptionQueue.add(e);
}
}
public boolean isLoaded() { return FLAG_LOADED; }
public Image getResource() { return resource; }
public Queue<Exception> getExcpetions() { return exceptionQueue; }
}
英文:
I am trying to design a framework for loading resources. So I have two options
-
Approach 1
Underlying structure: Aninterface
with a simpleabstract class
implementation and more specificextend
s.
Access: static final members of class initialized to the class. -
Approach 2
Underlying structure: A specificenum
from the get go, implementing all interface methods: no leveraging partial implementations of abstract classes.
Access: as enum entries
While approach 1 is a lot more flexible and reusable, I like the way enums provide a clean usable list, ready to use as opposed to static final
s with scope for Class.instance.instance.instance. ...
Is there a standard way to do this? Is there a better way to do this?
Though not strictly needed here's the code
Approach 1
Interface LoadableResource<T>
import java.util.ArrayDeque;
import java.util.Queue;
public interface LoadableResource<T> {
Queue<Exception> exceptionQueue=new ArrayDeque<>();
boolean load();//Load the resource and return status
boolean isLoaded() ;
T getResource();
void onLoadFail();
void onLoadSuccess();
void onException(Exception ex);
Queue<Exception> getExcpetions();
}
Abstract SimpleLoadableResource<T>
import java.util.Queue;
public abstract class SimpleLoadableResource<T> implements LoadableResource<T> {
private boolean FLAG_LOADED = false;
private T resource;
@Override
public boolean isLoaded() {
return FLAG_LOADED;
}
@Override
public T getResource() {
return resource;
}
@Override
public void onLoadFail() {}
@Override
public void onLoadSuccess() {}
@Override
public void onException(Exception ex) {exceptionQueue.add(ex); }
@Override
public Queue<Exception> getExcpetions() { return exceptionQueue; }
protected void setLoaded(boolean FLAG_LOADED) {
this.FLAG_LOADED = FLAG_LOADED;
}
public abstract T loader() throws Exception;
@Override
public boolean load() {
try {
resource=loader();
} catch (Exception e) { onException(e); }
if (isLoaded())
onLoadSuccess();
else
onLoadFail();
return isLoaded();
}
}
Specific SimpleLoadableImage
import javax.imageio.ImageIO;
import java.awt.*;
public class SimpleLoadableImage extends SimpleLoadableResource<Image>{
public static final SimpleLoadableImage LOGO1=new SimpleLoadableImage("1.jpg");
public static final SimpleLoadableImage LOGO2=new SimpleLoadableImage("2.jpg");
private final String path;
public SimpleLoadableImage(String path) {
this.path = path;
super.load();
}
@Override
public Image loader() throws Exception {
var res=ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(path));
setLoaded(true);
return res;
}
}
Approach 2
import javax.imageio.ImageIO;
import java.awt.*;
import java.util.ArrayDeque;
import java.util.Queue;
public enum SimpleLoadableImage_Enum {
LOGO1("1.jpg"),
LOGO("2.jpg");
private final String path;
Queue<Exception> exceptionQueue=new ArrayDeque<>();
private boolean FLAG_LOADED = false;
private Image resource;
private SimpleLoadableImage_Enum(String path){
this.path=path;
try {
resource=ImageIO.read(this.getClass().getClassLoader().getResourceAsStream(path));
FLAG_LOADED=true;
} catch (Exception e) {
exceptionQueue.add(e);
}
}
public boolean isLoaded() { return FLAG_LOADED; }
public Image getResource() { return resource; }
public Queue<Exception> getExcpetions() { return exceptionQueue; }
}
专注分享java语言的经验与见解,让所有开发者获益!
评论