dimanche 22 décembre 2019

Design pattern suggestion/implementation using java 8

I have to process requests in a series of steps.

For example: if request 1 comes, then I have to apply step1, step2, step3 and finally step4 which will persist the processed request into the database.

I thought of implementing Template design pattern, as it solves a similar problem.

When I started implementing the design pattern, I suddenly found it hard to implement, because of the logical complexity.

Let me explain the requirement:

Request -> Controller -> run()

The request will contain List.

Inside the run method, a series of operations will be fired.

 request.parallelStream()
.map(step1 -> executeStep1.apply(step1))
.map(step2 -> executeStep2.apply(step2, action))
.map(step3 -> executeStep3.apply(step3, rules))
.map(step4 -> executeStep4.apply(step4))
.collect(Collectors.toList());

Function<String, List<PersonDto>> executeStep1= person-> {
    return metaData.getMetaDataPerson(person);
};

BiFunction<List<PersonDto>, String, TransTemplateDTO> executeStep2= (metaData, action) -> {
    return temlate.createTemplate(metaData, action);

};



Now, as we can see I am passing the first element of request as an input to step1(), and then processing it and further passing the output as an input to subsequent steps.

Problem 1: Till this point, there was no problem. But suddenly the requirement changed and now I have to add rules logic in the step3 i.e. executeStep3.apply(step3).

step3 is taking 2 parameters, one is the output of step2 and second is List rules.
Step3 should apply all the rules and return the results equal to the rules.
For ex. If there are 3 rules, then step3 should return a List of 3 objects. Let's assume step3 received List of PersonDto with 10 items and List of rules of 3 items, then step3 should return 10*3 = 30 records. Also for each person rules will vary as per the command.
**Problem 2:** In step 3, I need the request object, so that I can make use of values. something like this:
**.map(step3 -> executeStep3.apply(step3, rules, request))**



Please help.

Aucun commentaire:

Enregistrer un commentaire