0

I have class Elevator which contains base informations about Elevator itself. Like here:

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

}

Now I want to separate behaviour of elevator from model, I want to create another class, maybe it will be implementing Runnable or Callable (doesn't matter now, it should be universal). It will have methods like these (prototype):

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

I really don't think that is correct like it is now, so my question is, which pattern should I use to separate information of object from run() methods etc. Should it be Decorator?

Thank you in advance!

MrFisherman
  • 720
  • 1
  • 7
  • 27

2 Answers2

1

I think your code looks fine. The whole point is to not have state in the behavioral classes. And you don't need a design pattern for that. Well, actually, you already used one: composition.

This specific case can be overly extended since "the elevator itself moves up/down" (this means the logic will go in Elevator) or "the controller handles the movement of the elevator" in which case a Controller class can have the behavior.

Also the localization field can be discussed since the position is not actually an elevator attribute, instead it can be a controller attribute.

tzortzik
  • 4,993
  • 9
  • 57
  • 88
  • Ok! Thank you then, I will move localization to Controller aslo. Cheers! – MrFisherman Feb 09 '21 at 21:11
  • @MrFisherman, please note that separating state from behavior is not object-oriented programming, so none of the GoF design patterns will apply, because those are object-oriented patterns. [The difference between object-oriented programming and procedural programming](https://stackoverflow.com/q/60116769/1371329) is that OO combines state and behavior into a single entity (an object). It's perfectly OK if you want to write procedural code; but you will want to look for procedural patterns rather than the GoF patterns. – jaco0646 Feb 09 '21 at 22:41
  • @jaco0646 don't we separate model from controller in mvc? Or like entity from service in spring? – MrFisherman Feb 09 '21 at 23:58
  • @MrFisherman, you're absolutely right. In enterprise (Java) programming, a lot (most) of the code is procedural, and the service/entity separation is the most common pattern. Service == procedure (all logic no data). Entity == data structure (all data no logic). Developers tend not to think about this, but OOP is relatively rare in enterprise Java (and becoming more rare with the increasing popularity of functional programming). Note the model from the original MVC architecture (not Spring) is a rich object containing all of the application's business logic. – jaco0646 Feb 10 '21 at 00:46
1

In some cases it can make sense to move behaviour somewhere else. In this case I think it makes more sense to keep everything in one class. An elevator has the given attributes and can move up and down. No need to make things complicated.

Also I find the name Runnable confusing. Runnable implies the class can be used to run some code. For example the interface Runnable (https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Runnable.html) can be extended to be run by a Thread.

magicmn
  • 1,787
  • 7
  • 15