educative.io

Why openButton and closedButton are of type ElevatorButton?

ElevatorButton has a attribute destinationFloorNumber but openButton and closedButton doesn’t need to have that attribute since they are not mapped to any particular destination floor?


Course: Grokking the Low Level Design Interview Using OOD Principles - Learn Interactively
Lesson: https://www.educative.io/courses/grokking-the-low-level-design-interview-using-ood-principles/code-of-elevator-system

Hello @Yash_Prajapati,

The openButton and closeButton in the ElevatorPanel are instances of the ElevatorButton class, which indeed has an attribute destinationFloorNumber . However, for the openButton and closeButton , the destination floor is not relevant, as they are meant for controlling the opening and closing of the elevator doors, not selecting a specific floor.

To address this, we need to create a separate class for buttons specifically for opening and closing doors without the destination floor attribute. For example:
public class DoorButton extends Button {
// No destination floor attribute needed for door buttons

    public boolean isPressed() {
        // Definition
    }
}

public class ElevatorPanel {
    private List<ElevatorButton> floorButtons;
    private DoorButton openButton;
    private DoorButton closeButton;
}

This way, we clearly distinguish between buttons used for floor selection (ElevatorButton ) and door control (DoorButton ). FYI, I have also updated the lesson accordingly.

Happy Learning :slight_smile: