-1

在我的应用程序中,我需要在运行时访问一些“关系”数据。如果我有很多数据或者经常更改,我会将其存储在数据库中(sqlite 或其他东西)。但我只有几个具有 5-50 个对象的实体,这些对象不会(经常)更改,并且仅由开发人员更改。因此,我认为在 Java 类中简单地对数据进行硬编码以简化代码维护是有意义的。

所有实体共享一个共同的抽象超类(具有 anint id和 a String name)。定义数据后,我希望引用每个实体对象和集合/数组,所有这些对象都在类之外可用。我想确保数据是不可变的。

第一个想法是做这样的事情:

public abstract class AbstractEntity{

    private final int id;
    private String name;

    protected AbstractEntity(int id, String name){
        this.id = id;
        this.name = name;
    }   

    private void someMethods(){
        ...
    }

    abstract protected void someAbstractMethods(){
        ...
    }
}

public final class MyEntity extends AbstractEntity{

    public static final ENTITY_1 = new MyEntity(1, "Entity 1", "foo");
    public static final ENTITY_2 = new MyEntity(2, "Entity 2", "bar");
    public static final ENTITY_3 = new MyEntity(3, "Entity 3", "baz");

    public static final Set<MyEntity> ALL = Collections.unmodifiableSet(new TreeSet<>(Arrays.asList(ENTITY_1, ENTITY_2, ENTITY_3)));

    private final String foo;

    private MyEntity(int id, String name, String foo){
        super(id, name);
        this.foo = foo;
    }
}

我看到的缺陷是,在开发过程中添加新实体时,我也必须将其添加到 ALL 集中。这让我很烦。这是一种冗余和错误的来源。

然后我只是想使用枚举作为实体类,我可以在其中定义实体对象并隐含地在 values() 数组中包含所有对象引用。但这不起作用,因为在 Java 中无法使用基类 (AbstractEntity) 和多重继承。

下一个想法是使用具有默认方法的接口而不是抽象基类,并作为实体定义一个实现该接口的枚举。但是我将无法在基类(接口)中定义 int id 和 String name,并且必须在每个子类中定义它。

我缺少一些语法糖吗?这里的最佳做法是什么?

4

2 回答 2

1

像这样的东西可以使用反射来解决问题。

public final class MyEntity extends AbstractEntity{

    public static final ENTITY_1 = new MyEntity(1, "Entity 1", "foo");
    public static final ENTITY_2 = new MyEntity(2, "Entity 2", "bar");
    public static final ENTITY_3 = new MyEntity(3, "Entity 3", "baz");

    public static final Set<MyEntity> ALL;

    static {
       final Set<MyEntity> all = new TreeSet<>();

       // Find all static fields in this class which are instances of this class:
       final Field[] fields = MyEntity.class.getDeclaredFields();
       for ( Field f : fields ) {
          if ( f.getType() == MyEntity.class ) {
            if ( Modifier.isStatic( f.getModifiers() ) {
               all.add((MyEntity)f.get(null));
            }
          }
       }

       ALL = Collections.unmodifiableSet( all );
    }

    private final String foo;

    private MyEntity(int id, String name, String foo){
        super(id, name);
        this.foo = foo;
    }
}

从中编写通用实用程序函数initializeEntitySet( Class<?> entityClass )应该是直截了当的。

于 2017-08-04T08:54:39.840 回答
1

正如评论中解释的那样,我宁愿使用 anEnum来存储每个实体的实例。您可以将参数添加到枚举构造函数,以便随后实例化和存储实体值。

接下来,我为您提供一个解决方案,让您Set存储所有实例,并Enum为 A 类型的实体和 B 类型的实体存储另一个(两者都继承自AbstractEntityALL 集中使用的实体)。ALL 集填充在从 ENUMS 中检索值的静态部分上,因此,您只需在枚举上添加条目即可添加新值。

实体.java

public class Entities {

    // Entities of class A
    private enum EntitiesOfA {
        ENTITY_A_1(1, "EntityA 1", "foo"), // A1
        ENTITY_A_2(2, "EntityA 2", "bar"), // A2
        ENTITY_A_3(3, "EntityA 3", "baz"); // A3

        private MyEntityA entity;


        private EntitiesOfA(int id, String name, String foo) {
            this.entity = new MyEntityA(id, name, foo);
        }

        public MyEntityA getEntity() {
            return this.entity;
        }
    }

    // Entities of class B
    private enum EntitiesOfB {
        ENTITY_B_1(4, "EntityB 1", 10), // B1
        ENTITY_B_2(5, "EntityB 2", 11), // B2
        ENTITY_B_3(6, "EntityB 3", 12); // B3

        private MyEntityB entity;


        private EntitiesOfB(int id, String name, int value) {
            this.entity = new MyEntityB(id, name, value);
        }

        public MyEntityB getEntity() {
            return this.entity;
        }
    }


    // All Entities
    public static final Set<AbstractEntity> ALL;

    static {
        // I use HashSet instead of TreeSet because I have
        // not implemented the comparable interface
        Set<AbstractEntity> allEntities = new HashSet<>();
        for (EntitiesOfA entity : EntitiesOfA.values()) {
            allEntities.add(entity.getEntity());
        }
        for (EntitiesOfB entity : EntitiesOfB.values()) {
            allEntities.add(entity.getEntity());
        }

        ALL = Collections.unmodifiableSet(allEntities);
    }


    public static void main(String[] args) {
        for (AbstractEntity entity : ALL) {
            System.out.println("Entity ID = " + entity.getId() + " NAME = " + entity.getName());
            entity.someAbstractMethods();

            if (entity instanceof MyEntityA) {
                MyEntityA a = (MyEntityA) entity;
                System.out.println("Entity A with foo = " + a.getFoo());
                a.someMethods();
            } else if (entity instanceof MyEntityB) {
                MyEntityB b = (MyEntityB) entity;
                System.out.println("Entity B with value = " + b.getValue());
                b.someMethods();
            } else {
                System.err.println("ERROR: Unrecognised subclass");
            }
        }
    }
}

抽象实体.java

public abstract class AbstractEntity {

    private final int id;
    private String name;


    protected AbstractEntity(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public void someMethods() {
        // A
    }

    protected abstract void someAbstractMethods();

}

MyEntityA.java 公共最终类 MyEntityA 扩展 AbstractEntity {

private final String foo;


public MyEntityA(int id, String name, String foo) {
    super(id, name);

    this.foo = foo;
}

public String getFoo() {
    return this.foo;
}

@Override
protected void someAbstractMethods() {
    // Some code
}

}

MyEntityB.java 公共最终类 MyEntityB 扩展 AbstractEntity {

private final int value;


public MyEntityB(int id, String name, int value) {
    super(id, name);

    this.value = value;
}

public int getValue() {
    return this.value;
}

@Override
protected void someAbstractMethods() {
    // Some code
}

}

请注意:

  • VAL(new MyEntityA(...))如果您愿意,可以将 ENUM 中的构造函数替换为 a 。在这种情况下,您可以合并EntitesOfAEntitesOfB拥有所有实体,并使用带有AbstractEntity. 你也可以删除Set

    public class AllEntities {
    
    private enum Entities {
        ENTITY_A_1(new MyEntityA(1, "EntityA 1", "foo")), // A1
        ENTITY_A_2(new MyEntityA(2, "EntityA 2", "bar")), // A2
        ENTITY_A_3(new MyEntityA(3, "EntityA 3", "baz")), // A3
        ENTITY_B_1(new MyEntityB(4, "EntityB 1", 10)), // B1
        ENTITY_B_2(new MyEntityB(5, "EntityB 2", 11)), // B2
        ENTITY_B_3(new MyEntityB(6, "EntityB 3", 12)); // B3
    
        private AbstractEntity entity;
    
    
        private Entities(AbstractEntity entity) {
            this.entity = entity;
        }
    
        public AbstractEntity getEntity() {
            return this.entity;
        }
    }
    
    
    // All Entities
    public static final Set<AbstractEntity> ALL;
    
    static {
        // I use HashSet instead of TreeSet because I have
        // not implemented the comparable interface
        Set<AbstractEntity> allEntities = new HashSet<>();
        for (Entities entity : Entities.values()) {
            allEntities.add(entity.getEntity());
        }
        ALL = Collections.unmodifiableSet(allEntities);
    }
    
  • 我添加了一个主要的测试方法,但您也可以将其删除。

于 2017-08-04T09:04:11.717 回答