public enum MethodType {
METHOD_A,
METHOD_B
}
public interface MethodStrategy {
void execute();
}
public class MethodAStrategy implements MethodStrategy {
@Override
public void execute() {
// methodA の実装
}
}
public class MethodBStrategy implements MethodStrategy {
@Override
public void execute() {
// methodB の実装
}
}
public class MethodExecutor {
public static void execute(MethodType methodType) {
MethodStrategy strategy;
switch (methodType) {
case METHOD_A:
strategy = new MethodAStrategy();
break;
case METHOD_B:
strategy = new MethodBStrategy();
break;
default:
throw new IllegalArgumentException("Invalid method type: " + methodType);
}
strategy.execute();
}
}
public class Test {
public static void main(String[] args) {
MethodExecutor.execute(MethodType.METHOD_A);
MethodExecutor.execute(MethodType.METHOD_B);
}
}
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme