The law of demeter - using only one point, is it possible to improve this logic?

I have the following method:

    private boolean reserveSeat(int selectedRow, int selectedSeat) {
    if (show.getRows().get(selectedRow).getSeats().get(selectedSeat).getReservationStatus()) {
        return false;
    } else {
        show.getRows().get(selectedRow).getSeats().get(selectedSeat).reserve();
        setRowNumber(selectedRow);
        setSeatNumber(selectedSeat);

        return true;
    }
}

which is in the reservation class. This class has a Show Object (show), A show has a string (another object), Rows have Seats (another object).

My question is, can this method be improved? I read about LoD and worry that my dot signals a bad design, although I think it is logical. This Seat object knows whether it is reserved or not. However, does she come from the show to Sit, talking with strangers? or is this normal due to how each object contains the next object?

, . , , (, , ), , , , , , !

.

+4
3

, . show , , . , . .

show, Seat Reservation, :

private boolean reserveSeat(int selectedRow, int selectedSeat) {
    if (show.isSeatReserved(selectedRow, selectedSeat)) {
        return false;
    } else {
        show.reserveSeat(selectedRow, selectedSeat);
        setRowNumber(selectedRow);
        setSeatNumber(selectedSeat);

        return true;
    }
}

, , show , , show, , .

+3

show , . show .

show show ( ).

show . , Row .

, Seat.


, show 2D-? .

. .

, , , .

+1

, . : → A ( ) , A , A Seat (s).

In the booking class, I now have this: Thanks @janos

private boolean reserveSeat(int selectedRow, int selectedSeat) {
    if (show.isSeatReserved(selectedRow, selectedSeat)) {
        System.out.println("Sorry, that seat has already been booked");
        return false;
    } else {
        show.reserveSeat(selectedRow, selectedSeat);
        setRowNumber(selectedRow);
        setSeatNumber(selectedSeat);
        System.out.println("This seat has now been booked.");
        return true;
    }
}

In the Show class, I have this:

public boolean isSeatReserved(int selectedRow, int selectedSeat) {
    if (getRow(selectedRow).getSeatStatus(selectedSeat)) {
        return true;
    } else
        return false;
}

and in the row class i

public boolean getSeatStatus(int selectedSeat) {
    return getSeat(selectedSeat).getReservationStatus();
}

I thought it might be useful for other people just starting out (like me) to show it graphically using before and after diagrams taken from a jarchitect tool that shows what a mess my code was in! I used the same logic to remove some other classes that "knew too much."

Before refactoring

After refactoring

0
source

All Articles