提问人:Valenciaaa- 提问时间:11/14/2023 最后编辑:Valenciaaa- 更新时间:11/14/2023 访问量:34
如何使用状态设计模式实现此 UML 图
How can I implement this UML Diagram using State Design Pattern
问:
在此处输入图像描述我有这个 UML 图,并希望使用 State Design Pattern 来实现 InProgressState、HelpState 和 EscapeState。 以下是详细信息:
- InProgressState:这是参与者的初始状态,参与者在解谜时会保持这种状态。
- 在此状态下,solveRiddle 检查输入是否解决了当前谜语。
- HelpState:当参与者通过调用 askForHelp 方法请求帮助时,将进入此状态。
- 在这种状态下,无论提供的输入如何,solveRiddle 都会解开谜语。 之后,如果还有剩余的谜语需要解决,参与者将被转换回 InProgressState,否则它们将被转换回 EscapedState。
- EscapedState:如果参与者成功解决了所有谜语,他们将被转换为 EscapedState。
- 在此状态下,如果调用 solveRiddle,则应引发 RiddleException。
注意:
- 如果谜语已解决,但作为参数再次传递给 solveRiddle,则应抛出 RiddleException。
- 参与者一次可以选择任何谜语来解决。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;
}
}
答:
0赞
Xinlong Wu
11/14/2023
#1
代码会像
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
}
评论
0赞
smgcuv587
11/14/2023
#2
您可以尝试读取这样的 .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;
}
评论