public interface Strategy{ public int doAction(int num1,int num2); }
1 2 3 4 5 6
public class OperationAdd implement Strategy{ @Override public int doAction(int num1, int num2){ return num1+num2; } }
1 2 3 4 5 6
public class OpeartionDel implement Strategy{ @Override public int doAction(int num1, int num2){ return num1-num2; } }
1 2 3 4 5 6 7 8 9 10 11
public class Operation{ private Strategy strategy; public Operation(Strategy strategy){ this.strategy = strategy; }
public int doAction(int num1, int num2){ return strategy.doAction(num1, num2); } }
1 2 3 4 5 6
public class Demo{ public static void main(String[] args){ Operation operation = new Operation(new OpeartionAdd()); System.out.println("10+5="+operation.doAction(10,5)) } }