vendredi 20 mars 2020

Choose service type based on Enum value

I would like to create kind of Factory Pattern, but am struggling.

//OperationType is an Enum
public interface SettlementService<OperationType>{
    void settleOperation(Order order);
}

then I have 2 services that handle different operation types in different ways.

public class ClientSettlementService implements SettlementService<OperationType.CLIENT> {
    @Override
    public void settleOperation(Order order) {

    }
}

public class CustomerSettlementService implements SettlementService<OperationType.CUSTOMER> {
    @Override
    public void settleOperation(Order order) {

    }
}

Then in my settlement method I have:


switch (order.getOperationType()) {
case OperationType.CLIENT:
    //here ClientSettlementService is called
    settlementService.settleOperation(order);
    break;
case OperationType.CUSTOMER:
    //here CustomerSettlementService is called
    settlementService.settleOperation(order);
    break;
default:
    LOG.error("Unsupported operation type.");
}

The code obviously doesn't work, but hope you get the idea. How do I do that?

Aucun commentaire:

Enregistrer un commentaire