mardi 14 novembre 2023

How can I implement this UML Diagram using State Design Pattern

enter image description here I have this UML Diagram and want to use State Design Pattern to implement InProgressState, HelpState and EscapeState. Following is the details:

  1. InProgressState: This is the initial state of the participant, and participants remain in this while they are solving the riddles.
  • Within this state, solveRiddle checks whether the input solves the current riddle.
  1. HelpState: This state is entered when the participant requests help by calling the askForHelp method.
  • In this state, solveRiddle will solve the riddle regardless of the input provided. After that, the participant will be transitioned back to InProgressState if there are remaining riddles to solve, otherwise they will be transitioned to EscapedState.
  1. EscapedState: If a participant successfully solves all riddles, they will be transitioned to the EscapedState.
  • In this state, RiddleException should be thrown if solveRiddle is called.

Note:

  1. If a riddle has been solved but passed as an argument to solveRiddle again, a RiddleException should be thrown.
  2. A participant can choose any riddle to solve at a time. The argument whichRiddle of the solveRiddle method indicates to which riddle he/she submits an input in the riddleList.

I have a Class called ParticipantState:

public interface ParticipantState {
    ParticipantState solveRiddle(String input, Riddle[] riddleList, int whichRiddle) throws RiddleException;
}

And I think InProgressState, HelpState and EscapedState should implements ParticipantState like:

public class InProgressState implements ParticipantState{

    @Override
    public ParticipantState solveRiddle(String input, Riddle[] riddleList, int whichRiddle) throws RiddleException {
        return this;
    }
}

Aucun commentaire:

Enregistrer un commentaire