jeudi 24 décembre 2020

template method design pattern - how to save and feed information into next step

I have an interface RequestProcessorInterface. There are different scenarios for processing a json request e.g. async vs synchronous request. I am trying to use template method pattern where the steps are like validate, preProcess, saveInDatabase, postProcess, etc


        ProcessingContext processingContext = validate(storeContentRequestContainer);
        processingContext = preProcess(storeContentRequestContainer, processingContext);
        saveInDatabase(storeContentRequestContainer, processingContext);
        return postProcess(storeContentRequestContainer, processingContext);

My ProcessingContext class has these attributes:

Map<String, String> errors;
String contentId;
String correlationId;
String presignedUrl;
String objectKey;
String bucketLocation;
DbEntity dbEntity; // Entity for database storage

When the json request is parsed, I assign values to 'processingContext' object. To keep the system flexible and not worry about what step might need the parsed information, I am encapsulating the extracted information in the context object.

Also I am passing the context object to every step so that in future, every step has this information readily available. I was going in the direction of most of the steps to be able to read the context and update some attribute and return the modified context , so the subsequent steps have access to attributes populated earlier.

I have a feeling that accepting context object (which is mutable) and modifying it and returning it is not a good idea. This context object is going to be in the method scope of a singleton class (spring boot). It will not be something lingering on forever and that should make it simpler.

How do I achieve this flexibility of multiple steps to be able to augment / update information in a context object? Will it make sense to make this context object immutable ?

Aucun commentaire:

Enregistrer un commentaire