1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ストラテジパターン

Posted at
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);
    }
}

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?