public class Order { public final static int FIXED_AMOUNT_COMMISION = 1; public final static int FIXED_PERCENT_COMMISION = 2; private BigDecimal quantity, price; private int commisionType; public Order(BigDecimal quantity, BigDecimal price, int commisionType) { this.quantity = quantity; this.price = price; this.commisionType = commisionType; } public BigDecimal getQuantity(){return quantity;} public BigDecimal getPrice() {return price;} public void setQuantity(BigDecimal quantity) { this.quantity = quantity; } public void setPrice(BigDecimal price) { this.price = price; } public BigDecimal calculateCommesion(){ switch (commisionType){ case FIXED_AMOUNT_COMMISION: return new BigDecimal("200.00"); case FIXED_PERCENT_COMMISION: return getQuantity().multiply(getPrice()).multiply(new BigDecimal(0.10)); } return new BigDecimal(0.00); } //other data and methods } 

It is necessary to change the design of the code so that it meets the following criteria:

  1. it is easy to add new types of commission and their calculus in the system without changing the class Order
  2. the ability to change the type of commission of this order after its creation;
  3. add the 3rd type of commission according to the following criteria:
    • if the amount is greater than this threshold (set for each instruction separately) the commission is 50.00;
    • otherwise, the commission is 100.00;
  4. Remove all firmly given numbers and replace them with parameters.
  • one
    you can pass a commissioning function for each created object, for example - iksuy
  • 3
    Strategy to help you - Dmitriy Simushev
  • Tell me how to implement the first item with the help of the Strategy strategy, given that in clause 4 we will need to pick up the given numbers ... - Alexander Dermenji
  • one
    @Alexander Dermenzhi, all criteria (including numerical ones) are in strategy. How exactly you will store them inside depends on the implementation. - Dmitriy Simushev

1 answer 1

 public class Order { private BigDecimal quantity, price; public Order(BigDecimal quantity, BigDecimal price) { this.quantity = quantity; this.price = price; } public BigDecimal getQuantity() { return quantity; } public BigDecimal getPrice() { return price; } public void setQuantity(BigDecimal quantity) { this.quantity = quantity; } public void setPrice(BigDecimal price) { this.price = price; } public BigDecimal calculateCommesion(Strategy strategy) { return strategy.calculate(this); } } public interface Strategy { BigDecimal calculate(Order order); } public class StrategyFixedAmount implements Strategy { private final BigDecimal value; public StrategyFixedAmount(BigDecimal value) { this.value = value; } @Override public BigDecimal calculate(Order order) { return value; } } public class StrategyFixedPercent implements Strategy { private final BigDecimal percent; public StrategyFixedPercent(BigDecimal percent) { this.percent = percent; } @Override public BigDecimal calculate(Order order) { return order.getQuantity().multiply(order.getPrice()).multiply(percent); } } public class StrategyThirdType implements Strategy { private final BigDecimal porog; private final BigDecimal before; private final BigDecimal after; public StrategyThirdType(BigDecimal porog, BigDecimal before, BigDecimal after) { this.porog = porog; this.before = before; this.after = after; } @Override public BigDecimal calculate(Order order) { return order.getQuantity().compareTo(porog) > 0 ? after : before; } } public class TestOrder { public static void main(String[] args) { Order order1 = new Order(new BigDecimal(10), new BigDecimal(100)); Order order2 = new Order(new BigDecimal(100), new BigDecimal(300)); Order order3 = new Order(new BigDecimal(40), new BigDecimal(700)); Strategy strategy1 = new StrategyFixedAmount(new BigDecimal(200)); Strategy strategy2 = new StrategyFixedPercent(new BigDecimal(0.1)); Strategy strategy3 = new StrategyThirdType(new BigDecimal(200), new BigDecimal(50), new BigDecimal(100)); System.out.println(order1.calculateCommesion(strategy1)); System.out.println(order1.calculateCommesion(strategy2)); System.out.println(order1.calculateCommesion(strategy3)); System.out.println(order2.calculateCommesion(strategy1)); System.out.println(order2.calculateCommesion(strategy2)); System.out.println(order2.calculateCommesion(strategy3)); System.out.println(order3.calculateCommesion(strategy1)); System.out.println(order3.calculateCommesion(strategy2)); System.out.println(order3.calculateCommesion(strategy3)); } }