public interface strategy {
abstract public int calculate(int a, int b);
}
public class addStrategy implements strategy {
@Override
public int calculate(int a, int b) {
// TODO Auto-generated method stub
return a + b;
}
}
public class substractStrategy implements strategy {
@Override
public int calculate(int a, int b) {
// TODO Auto-generated method stub
if(a > b){
return (a -b);
}else{
return (b - a);
}
}
}
public class multifyStrategy implements strategy {
@Override
public int calculate(int a, int b) {
// TODO Auto-generated method stub
return (a*b);
}
}
public class context {
private strategy strate;
public context(strategy strate){
super();
this.strate = strate;
}
public strategy getStrategy(){
return this.strate;
}
public void setStrategy(strategy strate){
this.strate = strate;
}
public int calculate(int a, int b){
return strate.calculate(a, b);
}
}
public class strategyTest {
public static void main(String[] args){
context con = new context(new addStrategy());
System.out.println(con.calculate(2, 3));
}
}