在最近的一次考试中,我被问到两个关于下面代码片段的问题.问题如下:
违反的设计原则。
中描述的设计模式的UML类图。
public class AI{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
String choice = input.nextLine();
if(choice.equals("some text"){
// do something
}
else if(choice.equals("another text"){
// do something
}
else if(choice.equals("extra text"){
// do something
}
...
else{
// do default
}
}
}发布于 2022-10-17 10:41:40
Your code violates - the Single Responsibility Principle (SRR)- design principle.
SRP make sure every class, module, or function in a program should have one responsibility/purpose in a program.
The strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.

上面是用于策略图的UML图。
https://stackoverflow.com/questions/63601002
复制相似问题