abstract strategy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public abstract class TaxStrategy{

  private final Double FEDERAL_TAX = 0.5d;

  public abstract Double calculate(Double subtotal);

  public static TaxStrategy createConcreteTaxA(){ //factory method used for concrete-strategy creation

    return new ConcreteTax(0.6d //provincialTax);

  }

}

concrete strategy A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class ConcreteTax extends TaxStrategy{

  private Double provincialTax;

  public ConcreteTax(Double provincialTax){
    this.provincialTax = provincialTax;
  }


  public Double calculate(Double subtotal){
    // complex calculation
    return //result of calculation
  }

}

client code that uses the strategy

1
2
3
4
TaxStrategy strategy = TaxStrategy.createConcreteTaxA();

Double result = strategy.calculateStrategy(100.58);