コメントで説明したように、むしろEnum
を使用して各エンティティのインスタンスを格納します。エンティティコンストラクタにパラメータを追加して、エンティティ値をインスタンス化して格納できるようにします。
次は、私はあなたのSet
は、すべてのインスタンスを格納するために維持するソリューションを提供するタイプAのentitesためEnum
とタイプBのエンティティのための別の1(両方ともALLセットで使用されてAbstractEntity
から継承)と。 ALLセットは、ENUMSから値を取得する静的な部分に格納されているため、新しい値を追加するために列挙型にエントリを追加する必要があります。
Entities.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");
}
}
}
}
AbstractEntity.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
}
}
注意それを拡張して延び
ENUMのコンストラクタは、必要に応じてVAL(new MyEntityA(...))
に置き換えることができます。この場合、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);
}
私はテストの主な方法を追加しましたが、削除することもできます。
これはまったく簡単ではありません。私はより良いアイデアは、データベースにそれらを保持し、最も頻繁に使用されるそれらのオブジェクトのためにメモリ内のキャッシュソリューションを提供することだと思います。 – duffymo
一部の限られたデータセットでdbソリューションを使用したくない場合は、代わりにプロパティファイルを使用します。このアプローチは、データベースを模倣するために多くの不必要な作業のように思えます。 – andrewdleach
エンティティをまったく作成しないでください。 ENUMを作成し、必要なフィールドを列挙型に追加し、実体に関係を格納するために列挙型を使用します。 – StanislavL