我如何(精美地)将这部分代码提取到子类?

时间:2014-10-18 06:47:38

标签: java optimization

我已经考虑了很多,但无法找到合适的解决方案。此外,很难谷歌这样的事情。

注意:代码本身是无关紧要的,关于如何做这样的事情。

我有一个名为Creature的类,它有以下方法:

private void move(int direction, LyxCamera camera, MapLayers mapLayers, ArrayList<Creature> entities) {
    if (!moving) {
        if (direction != NONE) {
            this.direction = direction;

            int goalGridPosX = gridPosX;
            int goalGridPosY = gridPosY;

            if (direction == UP) {
                goalGridPosY = gridPosY + 1;
            } else if (direction == RIGHT) {
                goalGridPosX = gridPosX + 1;
            } else if (direction == DOWN) {
                goalGridPosY = gridPosY - 1;
            } else if (direction == LEFT) {
                goalGridPosX = gridPosX - 1;
            }

            if (goalIsFree(goalGridPosX, goalGridPosY, mapLayers, entities)) {
                stop();
                moving = true;

                /* FROM HERE

                if (direction == UP) {
                    if (goalGridPosY > camera.getGridPosY() + 4) {
                        camera.move(direction, speed);
                    }
                } else if (direction == RIGHT) {
                    if (goalGridPosX > camera.getGridPosX() + 8) {
                        camera.move(direction, speed);
                    }
                } else if (direction == DOWN) {
                    if (goalGridPosY < camera.getGridPosY() + 4) {
                        camera.move(direction, speed);
                    }
                } else if (direction == LEFT) {
                    if (goalGridPosX < camera.getGridPosX() + 7) {
                        camera.move(direction, speed);
                    }
                }

                TO HERE*/

                gridPosX = goalGridPosX;
                gridPosY = goalGridPosY;

                step();
            }
        }
    }

我将要提取的代码标记为&#34; FROM HERE TO HERE&#34;。

我可以将代码放在方法中并调用empty方法,然后覆盖子类中的方法。但这似乎并不是一个合适的解决方案。我也可以复制整个方法,只删除超类中的标记区域。这似乎也不是最佳做法。

我确定你们确切地知道我在寻找什么。哦,如果您有改进代码的一般建议,请随时告诉我!

也许我已经太累了,在我的梦中会有一个解决方案。无论如何,我非常好奇地看到你的建议!

非常感谢!

关心Lyxodius

编辑:

首先,我非常感谢你们花时间给我这么详细的答案! 我的解决方案受到了来自这里和stackexchange的所有答案的启发!

超级&#34; Creature.java&#34;:

private void move(int direction, LyxCamera camera, MapLayers mapLayers,
        ArrayList<Creature> entities) {
    if (!moving && direction != NONE) {
        this.direction = direction;

        int goalGridPosX = gridPosX;
        int goalGridPosY = gridPosY;

        switch (direction) {
        case UP:
            goalGridPosY++;
            break;
        case RIGHT:
            goalGridPosX++;
            break;
        case DOWN:
            goalGridPosY--;
            break;
        case LEFT:
            goalGridPosX--;
            break;
        }

        if (goalIsFree(goalGridPosX, goalGridPosY, mapLayers, entities)) {
            prepareMoving(goalGridPosX, goalGridPosY);
        }
    }
}

protected void prepareMoving(int goalGridPosX, int goalGridPosY) {
    startMoving(goalGridPosX, goalGridPosY);
}

private void startMoving(int goalGridPosX, int goalGridPosY) {
    stop();

    moving = true;

    gridPosX = goalGridPosX;
    gridPosY = goalGridPosY;

    step();
}

子类&#34; Player.java&#34;:

@Override
protected void prepareMoving(int goalGridPosX, int goalGridPosY) {
    super.prepareMoving(goalGridPosX, goalGridPosY);
    moveCamera();
}

private void moveCamera() {
    if (direction == UP
            && gridPosY > camera.getGridPosY() + MINIMUM_VERTICAL_OFFSET) {
        camera.move(direction, speed);
    } else if (direction == RIGHT
            && gridPosX > camera.getGridPosX()
                    + MINIMUM_HORIZONTAL_OFFSET_RIGHT) {
        camera.move(direction, speed);
    } else if (direction == DOWN
            && gridPosY < camera.getGridPosY() + MINIMUM_VERTICAL_OFFSET) {
        camera.move(direction, speed);
    } else if (direction == LEFT
            && gridPosX < camera.getGridPosX()
                    + MINIMUM_HORIZONTAL_OFFSET_LEFT) {
        camera.move(direction, speed);
    }
}

2 个答案:

答案 0 :(得分:1)

对于一个简单的答案,您可以在基类中使用moveCamera保护方法并从此处调用它。

深入挖掘:

  • 您认为基类可以代表什么?这个班 代表一个生物。基类是'GameObject'吗?
  • 此外,可以将moveCamera方法移动到相机 object作为 move 方法?

考虑类的最佳方式是它所代表的真实对象。与每个对象相关的逻辑应该是该类的一部分。你正在做的(+4)(+ 8)等动作看起来应该是相机类的一部分。

答案 1 :(得分:0)

我认为这是一个用于抽象类的完美示例。当知道某些功能时,应该使用抽象类。

public abstract class Creature{

    private void move(int direction, LyxCamera camera, MapLayers mapLayers,    
        ArrayList<Creature> entities) {
        if (!moving) {
            if (direction != NONE) {
                this.direction = direction;

                int goalGridPosX = gridPosX;
                int goalGridPosY = gridPosY;

                if (direction == UP) {
                    goalGridPosY = gridPosY + 1;
                } else if (direction == RIGHT) {
                    goalGridPosX = gridPosX + 1;
                } else if (direction == DOWN) {
                    goalGridPosY = gridPosY - 1;
                } else if (direction == LEFT) {
                    goalGridPosX = gridPosX - 1;
                }

                if (goalIsFree(goalGridPosX, goalGridPosY, mapLayers, 
                    entities)) {

                    stop();
                    moving = true;

                    // call the abstract method to be defined later 
                    //   (in sub classes)
                    doSomething(direction, goalGridPosX, goalGridPosY, 
                        camera);

                    gridPosX = goalGridPosX;
                    gridPosY = goalGridPosY;

                    step();
                }
            }
        }
    }

    protected abstract void doSomething(final int direction, 
        final int goalGridPosX, final int goalGridPosY, 
        final LyxCamera camera);
}