package com.strategy;
public interface Strategy {
double compute();
}
package com.strategy;
public class AddStrategy implements Strategy {
private Integer a;
private Integer b;
public AddStrategy(Integer a, Integer b) {
super();
this.a = a;
this.b = b;
}
@Override
public double compute() {
return a + b;
}
public Integer getA() {
return a;
}
public void setA(Integer a) {
this.a = a;
}
public Integer getB() {
return b;
}
public void setB(Integer b) {
this.b = b;
}
}
package com.strategy;
public class SubStrategy implements Strategy {
private Integer a;
private Integer b;
public SubStrategy(Integer a, Integer b) {
super();
this.a = a;
this.b = b;
}
@Override
public double compute() {
return a - b;
}
public Integer getA() {
return a;
}
public void setA(Integer a) {
this.a = a;
}
public Integer getB() {
return b;
}
public void setB(Integer b) {
this.b = b;
}
}
package com.strategy;
public class Manager {
private Strategy stragety;
public Manager(Strategy stragety) {
super();
this.stragety = stragety;
}
public double start() {
return stragety.compute();
}
public Strategy getStragety() {
return stragety;
}
public void setStragety(Strategy stragety) {
this.stragety = stragety;
}
}
package com.strategy;
public class Test {
public static void main(String[] args) {
Manager m = new Manager(new AddStrategy(2, 3));
System.out.println(m.start());
double pi = new Manager(new Strategy() {
@Override
public double compute() {
return Math.PI;
}
}).start();
System.out.println(pi);
}
}