mercredi 25 mars 2020

Java: How to run a method C of a class with methods A,B,C if methods A and B of this class are never called?

So I (newbie) am working with state design pattern and having trouble in interchanging states. For reference, there is a door, that can be in four states-closed, closing, opened, opening or pause state. So in the code that I am attaching , I am supposed to do this - door starts in closed state, up is pushed, now the door is in opening state. Now my problem is, I am not sure how to do this - when the door is in the opening state, if no button was pushed on the remote control, its state should be changed to opened. I am attaching the code for both closed and opening state classes. Any help would be appreciated. Opening State code:

public class OpeningState implements StateDoor{
GarageDoor door;
public OpeningState(GarageDoor door) {
    this.door=door;
    fullyOpen();
}
@Override
public void fullyOpen() {
    door.setState(door.OpenedState);
}

@Override
public void fullyClosed() {
    System.out.println("Can't close fully right now.");

}

@Override
public void upPushed() {
;   
}

@Override
public void downPushed() {
    door.setState(door.PauseState);
    System.out.println("Now changing to pause state from opening state.");
    }
public String toString() {
    return "OpeningState";
}


}

Closed State code:

public class ClosedState implements StateDoor{
GarageDoor door;
public ClosedState(GarageDoor door) {
this.door=door;
door.setState(door.ClosedState);
}
@Override
public void fullyOpen() {
    System.out.println("Can't open fully right now.");
}

@Override
public void fullyClosed() {
    System.out.println("Already fully closed.");
}

@Override
public void upPushed() {
    door.setState(door.OpeningState);
System.out.println("Now entering opening from closed state");
}

@Override
public void downPushed() {
    ;

}
public String toString() {
    return "ClosedState";
}


}

Aucun commentaire:

Enregistrer un commentaire