java 如何使用状态设计模式实现此UML图

ergxz8rk  于 2023-11-15  发布在  Java
关注(0)|答案(2)|浏览(103)

enter image description here我有这个UML图,我想用状态设计模式来实现InProgressState、HelpState和EscapeState,下面是详细信息:

  1. InProgressState:这是参与者的初始状态,参与者在解谜语时仍保持在此状态。
  • 在这个状态下,solveRiddle检查输入是否解决了当前的谜语。
  1. HelpState:当参与者通过调用askForHelp方法请求帮助时,进入此状态。
  • 在此状态下,solveRiddle将解决谜题,而不管提供了什么输入。之后,如果还有谜题要解决,参与者将转换回InProgressState,否则将转换到EscapedState。
  1. EscapedState:如果参与者成功解决了所有谜语,他们将转换到EscapedState。
  • 在此状态下,如果调用solveRiddle,则应抛出RiddleException。

注意事项:
1.如果一个谜语已经解开,但作为参数再次传递给solveRiddle,则会抛出一个RiddleException。
1.一个参与者可以选择任何一个谜语来解决。solveRiddle方法的参数whichRiddle表示他/她在riddleList中提交了一个输入给哪个谜语。
我有一个名为ParticipantState的类:

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

字符串
我认为InProgressState、HelpState和EscapedState应该实现如下的ParticipantState:

public class InProgressState implements ParticipantState{

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

hmae6n7t

hmae6n7t1#

代码将像

public class HelpState implements ParticipantState{

@Override
public ParticipantState solveRiddle(String input, Riddle[] riddleList, int whichRiddle) throws RiddleException {
    Riddle riddle = riddleList[whichRiddle];
    if (riddle.isSolved())
        throw new RiddleException("Already Solved");

    riddle.setSolved();
    return new InProgressState();
}
}

字符串

public class InProgressState implements ParticipantState {
@Override
public ParticipantState solveRiddle(String input, Riddle[] riddleList, int whichRiddle) throws RiddleException {
    Riddle riddle = riddleList[whichRiddle];
    if (riddle.isSolved())
        throw new RiddleException("Already Solved");
    return this;
}
}


public class EscapedState implements ParticipantState{

@Override
public ParticipantState solveRiddle(String input, Riddle[] riddleList, int whichRiddle) throws RiddleException {
    throw new RiddleException("Alseady Escaped");
}
}


public void solveRiddle(String input, int whichRiddle) throws RiddleException {
    this.state = this.state.solveRiddle(input, riddleList, whichRiddle);

    // TODO  - Your Code Starts here

    boolean isEsacped = true;
    for (Riddle item :
            riddleList) {
        if (item.isSolved() != true) {
            isEsacped = false;
            break;
        }
    }
    if (isEsacped){
        this.state = new EscapedState();
        for (Observer observer:
                observers) {
            observer.updateEscape(this);
        }
    }

    //TODO - Your Code Ends here
}

nkkqxpd9

nkkqxpd92#

你可以尝试像这样读取一个.csv文件(在本例中,我们将数据加载到Person类的列表中作为返回值):

public List<Person> readCsv(String fileName) {

    List<Person> list = new LinkedList<>();

    BufferedReader bufferedReader;
    File csv=new File(fileName);
    try{
        bufferedReader=new BufferedReader(new FileReader(csv));
        String buffer="";
        buffer=bufferedReader.readLine();//read the first line
        while (buffer!=null){
            String[] s=buffer.split(";");
            Person p =new Person(Integer.valueOf(s[0]),s[1],Integer.valueOf(s[2]),s[3]);
            list.add(p);
            buffer=bufferedReader.readLine();
        }
    }catch (Exception e){
        System.out.println("error in readcsv!");
    }

    return list;
}

字符串

相关问题