gpt4 book ai didi

java - 工厂模式 - 在 SQL 存储库中构建然后构建?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:33:52 26 4
gpt4 key购买 nike

我正在开发一款包含多种不同类型积木的网页游戏,因此我决定实现工厂模式。砖 block 存储在数据库中(必须有),然后使用带有结果集中属性的工厂进行构建。

工厂:

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);

具体实现:

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 ResultSet to Brick 由 getAll, getByName, ... 在存储库中使用

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,其中包含数据库中所有不同类型的积木。我想从这个数组中取出一 block 积木,例如 EnergyBrick,然后创建一个具有相同属性的新 EnergyBrick,并在这个积木上使用构造方法来使用 hitCount 设置 x 和 y 坐标。

我尝试过的事情:

databaseBricks.get(brickIndex) 然后在这 block 砖上使用构造方法,问题是所有的砖都有相同的引用

repository.getByName("energy") 工作得很好,但是查询数据库的时间太长了

最佳答案

您已经构建了包含所有必需数据的数据库,但尚未构建它来支持您的查询模式。

如果查询特定的砖 block ,您应该在构成唯一砖 block 的字段上有一个索引。

and then create a new EnergyBrick with the same properties and use the construct method on this brick to set the x and y coordinates with the hitCount.

所以,使用你的工厂。你创建它是为了让新积木的 build 更容易。使用它!

Brick brick = getBrickToBeCopied();
Brick newBrick = BrickBuilder.build(brick.getName(), brick.getWidth(), brick.getHeight(), brick.getImage());
database.store(newBrick);

或者如果宽度和高度改变了

Brick brick = getBrickToBeCopied();
Brick newBrick = BrickBuilder.build(brick.getName(), newWidth, newHeight, brick.getImage());
database.store(newBrick);

我看到一件令人困惑的事情,似乎 X 和 Y 位置的宽度和高度混淆了。如果您的数据库存储位置,则将其添加到 Brick 对象并在数据库中维护它。虽然位置和维度都有 (x, y) 值对,但它们代表不同的事物,交换它们不会导致预期的结果。

关于java - 工厂模式 - 在 SQL 存储库中构建然后构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47527332/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com