【发布时间】:2021-05-13 12:35:08
【问题描述】:
我有类 Elevator,其中包含有关 Elevator 本身的基本信息。喜欢这里:
@Builder
@Getter
@Setter
public class Elevator {
private final int id;
private final float maxSpeed;
private final float maxLiftingCapacity;
private float currentSpeed;
private float currentConditionFactor;
private Dimensions dimensions;
private Localization localization;
}
现在我想将电梯的行为与模型分开,我想创建另一个类,也许它会实现 Runnable 或 Callable(现在没关系,它应该是通用的)。它会有这样的方法(原型):
public class ElevatorRunnable implements Sleepable {
private final Elevator elevator;
public ElevatorRunnable(Elevator elevator) {
this.elevator = elevator;
}
private void moveUp() {
float posY = elevator.getLocalization().getY();
if (posY >= elevator.getBuilding().getGroundHeight()) {
elevator.getLocalization().setY(posY - elevator.getCurrentSpeed());
}
}
private void moveDown() {
float posY = elevator.getLocalization().getY();
if (posY <= elevator.getBuilding().getHeight()) {
elevator.getLocalization().setY(elevator.getLocalization().getY() + elevator.getCurrentSpeed());
}
}
我真的不认为现在这样是正确的,所以我的问题是,我应该使用哪种模式将对象信息与 run() 方法等分开。它应该是装饰器吗?
提前谢谢你!
【问题讨论】:
标签: java design-patterns decorator command-pattern