设计资源访问:最终枚举 vs 可扩展类的静态成员

huangapple 未分类评论45阅读模式
英文:

Designing resource access: Final enums vs static members of extendable classes

问题

我正在尝试设计一个用于加载资源的框架。所以我有两个选项:

  1. 方法 1
    底层结构: 一个带有简单的抽象类实现和更具体的扩展接口
    访问方式: 类的静态常量成员初始化为该类。

  2. 方法 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

  1. Approach 1
    Underlying structure: An interface with a simple abstract class implementation and more specific extends.
    Access: static final members of class initialized to the class.

  2. Approach 2
    Underlying structure: A specific enum 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 finals 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&lt;T&gt;

import java.util.ArrayDeque;
import java.util.Queue;

public interface LoadableResource&lt;T&gt; {
    Queue&lt;Exception&gt; exceptionQueue=new ArrayDeque&lt;&gt;();    
    boolean load();//Load the resource and return status
    boolean isLoaded() ;    
    T getResource();
    void onLoadFail();
    void onLoadSuccess();
    void onException(Exception ex);
    Queue&lt;Exception&gt; getExcpetions();
}

Abstract SimpleLoadableResource&lt;T&gt;

import java.util.Queue;

public abstract   class SimpleLoadableResource&lt;T&gt; implements LoadableResource&lt;T&gt; {

    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&lt;Exception&gt; 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&lt;Image&gt;{

    public static final SimpleLoadableImage LOGO1=new SimpleLoadableImage(&quot;1.jpg&quot;);
    public static final SimpleLoadableImage LOGO2=new SimpleLoadableImage(&quot;2.jpg&quot;);

    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(&quot;1.jpg&quot;),
    LOGO(&quot;2.jpg&quot;);

    private final String path;
    Queue&lt;Exception&gt; exceptionQueue=new ArrayDeque&lt;&gt;();
    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&lt;Exception&gt; getExcpetions() {  return exceptionQueue;  }
}

huangapple
  • 本文由 发表于 2020年7月27日 07:13:27
  • 转载请务必保留本文链接:https://java.coder-hub.com/63106733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定