여러 종류의 벽돌이있는 웹 게임에서 작업 중이므로 공장 패턴을 구현하기로 결정했습니다. 벽돌은 데이터베이스에 저장되며 결과 세트의 특성을 사용하여 팩토리를 사용하여 빌드됩니다.팩토리 패턴 - SQL 리포지토리에 빌드하고 나중에 구성 할 수 있습니까?
공장 :
public class BrickBuilder {
public Brick build(String type, int width, int height, String image) {
Brick brick = null;
switch (type) {
case "energy":
brick = new EnergyBrick(width, height, image);
break;
...
return brick;
}
부모 클래스 :
public abstract class Brick {
// Properties
public Brick(String type, int width, int height, String image){
this.type = type;
this.width = width;
this.height = height;
this.image = image;
}
public abstract void construct(double x, double y, int hitCount);
콘크리트 구현 : getAll 메, getByName에서 사용하는 벽돌에
public class EnergyBrick extends Brick {
// Properties
public EnergyBrick(int width, int height, String image) {
super("energy", width, height, image);
}
@Override
public void construct(int hitCount, double x, double y) {
this.hitCount = hitCount;
this.x = x;
this.y = y;
}
SQL 결과, ... 저장소
에서private Brick rsToBrick(ResultSet rs) throws SQLException {
String type = rs.getString("name");
int width = rs.getInt("width");
int height = rs.getInt("height");
String image = rs.getString("image");
Brick brick = BUILDER.build(type, width, height, image);
return brick;
}
벽돌
private void generateBricks() {
List<Brick> databaseBricks = BrickRepositorySQL.getInstance().getAll();
bricks = new ArrayList<>();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 14; j++) {
chance = Math.floor(Math.random() * 5);
if (chance == 4) {
continue;
}
if (chance == 3 || chance == 2 || chance == 1) {
// Create new concrete brick with tesame properties as in the array
x = j * 140;
y = i * 80;
brick.construct(x, y, 1);
if (chance == 0) {
x = j * 140;
y = i * 80;
// Create new concrete brick with tesame properties as in the array
brick.construct(x, y, 1);
}
bricks.add(brick);
}
}
}
}
나는 데이터베이스에서 벽돌의 모든 다른 유형을 가진 ArrayList에이를 생성합니다. 이 배열에서 브릭 중 하나를 가져오고 싶습니다. 예를 들어 EnergyBrick과 같은 속성을 가진 새로운 EnergyBrick을 만들고이 벽돌의 구성 메서드를 사용하여 x 및 y 좌표를 hitCount로 설정하고 싶습니다.
것들 내가 시도 :
databaseBricks.get (brickIndex) 다음이 벽돌에 구조 방법을 사용, 문제는 모든 벽돌이 같은 기준을
repository.getByName ("에너지"를 가지고있다) 완벽하게 작동하지만 각 브릭에 대해 데이터베이스를 쿼리하는 데 너무 오래 걸린다
귀하의 질문은 무엇입니까? 당신은 DB를 쿼리하고 값을 캐시해야 할 것 같은데? –
'brick.construct (x, y, 1);'벽돌 선언 위치는 어디입니까? BTW는 JPA를 사용하거나 적어도 Hibernate를 사용하고 벽돌을 automap 할 수 없습니까? –
DiscriminatorColumn, DiscriminatorValue 및 팩토리 메서드, DAO 메서드 또는 기타가 필요하지 않습니다. https://stackoverflow.com/questions/35872402/why-is-discriminatorcolumn-ignored-when-using-jointable –