当前位置: 首页 > 面试题库 >

设计和对象责任的问题

厍胤运
2023-03-14
问题内容

我有一个与设计和对象结构相关的问题。这是问题说明:

  1. 我有一个机器人对象,该对象应该自行穿越地面。将提供运动指令,并且必须相应地进行解析。例如,示例输入为:向右旋转|向左移动|向左旋转|向左移动

移动是网格上的单位移动。

我在Java中做了一个非常基本的设计。(下面粘贴完整的代码)

package com.roverboy.entity;

import com.roverboy.states.RotateLeftState;
import com.roverboy.states.RotateRightState;
import com.roverboy.states.State;

public class Rover {

    private Coordinate roverCoordinate;
    private State roverState;

    private State rotateRight;
    private State rotateLeft;
    private State move;

    public Rover() {
        this(0, 0, Compass.NORTH);
    }

    public Rover(int xCoordinate, int yCoordinate, String direction) {
        roverCoordinate = new Coordinate(xCoordinate, yCoordinate, direction);
        rotateRight = new RotateRightState(this);
        rotateLeft = new RotateLeftState(this);
        move = new MoveState(this);
    }

    public State getRoverState() {
        return roverState;
    }

    public void setRoverState(State roverState) {
        this.roverState = roverState;
    }

    public Coordinate currentCoordinates() {
        return roverCoordinate;
    }

    public void rotateRight() {
        roverState = rotateRight;
        roverState.action();
    }

    public void rotateLeft() {
        roverState = rotateLeft;
        roverState.action();
    }

    public void move() {
        roverState = move;
        roverState.action();
    }
}


package com.roverboy.states;

public interface State {

    public void action();
}

package com.roverboy.entity;

import com.roverboy.states.State;

public class MoveState implements State {

    private Rover rover;

    public MoveState(Rover rover) {
        this.rover = rover;
    }

    public void action() {
        rover.currentCoordinates().setXCoordinate(
                (Compass.EAST).equalsIgnoreCase(rover.currentCoordinates()
                        .getFacingDirection()) ? rover.currentCoordinates()
                        .getXCoordinate() + 1 : rover.currentCoordinates()
                        .getXCoordinate());

        rover.currentCoordinates().setXCoordinate(
                (Compass.WEST).equalsIgnoreCase(rover.currentCoordinates()
                        .getFacingDirection()) ? rover.currentCoordinates()
                                .getXCoordinate() - 1 : rover.currentCoordinates()
                                .getXCoordinate());

        rover.currentCoordinates().setYCoordinate(
                (Compass.NORTH).equalsIgnoreCase(rover.currentCoordinates()
                        .getFacingDirection()) ? rover.currentCoordinates()
                                .getYCoordinate() + 1 : rover.currentCoordinates()
                                .getYCoordinate());

        rover.currentCoordinates().setYCoordinate(
                (Compass.SOUTH).equalsIgnoreCase(rover.currentCoordinates()
                        .getFacingDirection()) ? rover.currentCoordinates()
                                .getYCoordinate() - 1 : rover.currentCoordinates()
                                .getYCoordinate());
    }
}


package com.roverboy.states;

import com.roverboy.entity.Rover;

public class RotateRightState implements State {

    private Rover rover;

    public RotateRightState(Rover rover) {
        this.rover = rover;
    }

    public void action() {
        rover.currentCoordinates().directionOnRight();
    }

}

package com.roverboy.states;

import com.roverboy.entity.Rover;

public class RotateLeftState implements State {

    private Rover rover;

    public RotateLeftState(Rover rover)
    {
        this.rover = rover;
    }

    public void action() {
        rover.currentCoordinates().directionOnLeft();
    }

}


package com.roverboy.entity;

public class Coordinate {

    private int xCoordinate;
    private int yCoordinate;
    private Direction direction;
    {
        Direction north = new Direction(Compass.NORTH);
        Direction south = new Direction(Compass.SOUTH);
        Direction east = new Direction(Compass.EAST);
        Direction west = new Direction(Compass.WEST);
        north.directionOnRight = east;
        north.directionOnLeft = west;
        east.directionOnRight = north;
        east.directionOnLeft = south;       
        south.directionOnRight = west;
        south.directionOnLeft = east;
        west.directionOnRight = south;
        west.directionOnLeft = north;
        direction = north;
    }

    public Coordinate(int xCoordinate, int yCoordinate, String direction) {
        this.xCoordinate = xCoordinate;
        this.yCoordinate = yCoordinate;
        this.direction.face(direction);
    }

    public int getXCoordinate() {
        return xCoordinate;
    }
    public void setXCoordinate(int coordinate) {
        xCoordinate = coordinate;
    }
    public int getYCoordinate() {
        return yCoordinate;
    }
    public void setYCoordinate(int coordinate) {
        yCoordinate = coordinate;
    }

    public void directionOnRight()
    {
        direction.directionOnRight();
    }

    public void directionOnLeft()
    {
        direction.directionOnLeft();
    }

    public String getFacingDirection()
    {
        return direction.directionValue;
    }
}

class Direction
{
    String directionValue;
    Direction directionOnRight;
    Direction directionOnLeft;

    Direction(String directionValue)
    {
        this.directionValue = directionValue;
    }

    void face(String directionValue)
    {
        for(int i=0;i<4;i++)
        {
            if(this.directionValue.equalsIgnoreCase(directionValue))
                break;
            else
                directionOnRight();
        }
    }

    void directionOnRight()
    {
        directionValue = directionOnRight.directionValue;
        directionOnRight = directionOnRight.directionOnRight;
        directionOnLeft = directionOnRight.directionOnLeft;             
    }

    void directionOnLeft()
    {
        directionValue = directionOnLeft.directionValue;
        directionOnRight = directionOnLeft.directionOnRight;
        directionOnLeft = directionOnLeft.directionOnLeft;      
    }
}

现在,我对最后一类“方向”和“坐标”感到怀疑。坐标代表流动站的坐标对象,可帮助其保持方向。目前,为了跟踪方向,我使用了双向链接的Direction对象列表,该列表几乎像指南针一样工作。向左或向右旋转。

这是我的问题。1.我使用状态模式并显示了用于方向跟踪的设计。是否有更好的方法来简化这一点?雷姆 我需要正确维护坐标;这样,如果您朝+
y轴移动,我的坐标应该在+ else处为负。X轴相同。

  1. 目前,改变流动站表面的职责间接地委托给坐标和方向类。这是真的吗?漫游者不负责维持方向吗?我在设计中是否真的正确地将这种责任委托给了协调和指导班级?仅仅因为在那里更容易操纵它?

  2. 任何对代码的简单设计改进和建议都将受到欢迎。随时批评。

感谢您的耐心配合和反馈;提前。


问题答案:

您在问如何简化。如果我可以建议一些粗体,为什么不使用一个不透明的int作为方向,并使用一个静态类来处理呢?“不透明的int”是指您的代码永远不会直接使用它,而只是将其作为Direction类的参数。

这是一些部分的Java风格的伪代码,以显示我的意思。

// 0 = east, 1 = north, 2 = west, ...
public class Direction {
  static int [] moveX = [ 1, 0, -1, 0];
  static final int NORTH = 1;
  // coordinates after moving one step in the given direction
  static Pair move(int direction, Pair old) {
     return new Pair( old.x + moveX[direction] , old.y + moveY[direction] );
  }
  static int turnLeft(int direction) { 
     return (direction+1) % 4;
  }
  static int turnRight(int direction) {
     return (direction+3) % 4;
  }
}

这种处理方式将具有使用较少分配的优势,因此垃圾收集器将不需要经常运行。另一个优点是,设计保持面向对象的意义,即如果以后您希望一次能够旋转45度,则可以轻松更改方向类别。

为了回答您的其他问题,我认为将Directions沿某个方向更改的任务委托给Direction类是完全可以的。流动站仅在流动站对象将包含一个int字段来存储其面对的方向的意义上负责保持方向。



 类似资料:
  • 责任链(Chain Of Responsibility) Intent 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链发送该请求,直到有一个对象处理它为止。 Class Diagram Handler:定义处理请求的接口,并且实现后继链(successor) Implementation // java public abstract c

  • 本文向大家介绍Java责任链设计模式,包括了Java责任链设计模式的使用技巧和注意事项,需要的朋友参考一下 责任链(Chain of Responsibility)模式是一种对象的行为模式。在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上 传递,直到链上的某一个对象决定处理此请求。发出这个请求的客户端并不知道链上的哪一个对象最终处理这个请求,这使得系统可以在不

  • 本文向大家介绍Java责任链设计模式实例分析,包括了Java责任链设计模式实例分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Java责任链设计模式。分享给大家供大家参考,具体如下: 一 代码 二 运行 h1:责任经过我头上!! h1:我把责任交给了h2 h2:责任经过我头上!! h2:我把责任交给了h3 h3:责任经过我头上!! h3我处理了责任!! 三 类图 更多java相关内容感

  • 单一职责原则是最简单的面向对象设计原则,它用于控制类的粒度大小。单一职责原则定义如下: 单一职责原则(Single Responsibility Principle, SRP):一个类只负责一个功能领域中的相应职责,或者可以定义为:就一个类而言,应该只有一个引起它变化的原因。 单一职责原则告诉我们:一个类不能太“累”!在软件系统中,一个类(大到模块,小到方法)承担的职责越多,它被复用的可能性就越小

  • 本文向大家介绍学习JavaScript设计模式之责任链模式,包括了学习JavaScript设计模式之责任链模式的使用技巧和注意事项,需要的朋友参考一下 一、定义 责任链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系,将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。 二、示例 假设这么一个场景: 我们负责一个售卖手机的电商网站,经过分别缴纳500

  • 本文向大家介绍Java设计模式之责任链模式简介,包括了Java设计模式之责任链模式简介的使用技巧和注意事项,需要的朋友参考一下 对于使用过宏的朋友应该知道,利用宏可以实现一个键绑定多个技能。例如如果排在前面的技能有CD,则跳过此技能,执行之后的技能。记得曾经玩DK,打怪的时候,就是用一个键,一直按就行了。在servlet里的doGet和doPost方法,我们一般都把doGet请求发动到doPost