正如评论中解释的那样,我宁愿使用 anEnum
来存储每个实体的实例。您可以将参数添加到枚举构造函数,以便随后实例化和存储实体值。
接下来,我为您提供一个解决方案,让您Set
存储所有实例,并Enum
为 A 类型的实体和 B 类型的实体存储另一个(两者都继承自AbstractEntity
ALL 集中使用的实体)。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 。在这种情况下,您可以合并EntitesOfA
并EntitesOfB
拥有所有实体,并使用带有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);
}
我添加了一个主要的测试方法,但您也可以将其删除。