vendredi 17 août 2018

What is the best design to implement a different flow according to a value?

thanks in advance for your help. I'm designing a new Java Spring Boot based application in charge of returning a state according to an input parameter. According to this input parameter, to achieve the final state, a different flow has to be made, although sometimes they share functionalities between flows.

I'm thinking about the best way to implement that idea and I've been looking for different desing patterns, but I've not found the correct desig, I'm probably a bad seeker...

The application receive a request by REST endpoint and handle it. My idea is the following:

1) Create an interface with that involve main funcionality:

@FunctionalInterface
public interface StatusService {
    Response getStatus(Request request);
}

2) Implements main funcionality and call to a class selector:

@Service
public class StatusServiceImpl implements StatusService {

   @Autowired // Yes, constructor injection it's better ;)
   private Selector selector; 

    @Override
    public Response getStatus(Request request) {
        validateRequest(request);
        UserInfo userInfo = getCommonInfoByRequest(request);
        return selector.getStatusSelectingWorkFlow(userInfo);
    }
}

3) Implements selector with a control flow statement:

@Component
public class Selector {

   @Autowired
   private Selector selector; 

    public Response getStatusSelectingWorkFlow(UserInfo userInfo) {
        System.out.println("Selecting workflow by param from request");
        switch (WorkFlowsEnum.valueOf(request.getParam())) {
        case A:
            return // Workflow 'A'
        case B:
             return // Workflow 'B'
        case C:
        case D:
             return // Workflow 'C' and 'D'
        default:
           System.out.println("Unconsidered workflow");
           break;
         }
    }
 }

Could someone give me their point of view on this idea or a better alternative?

Thank you for your time

Aucun commentaire:

Enregistrer un commentaire