I have a huge project. Inside, I have many flows, such as purchasing flow, search flow etc. I am aiming to publish logs inside these flows to S3 (for simplicity, S3 is not relevant here.)

I have a data class as following:

@Builder(toBuilder = true)
public class FlowLogDto {

private Flow flow; // Purchasing Flow, Search Flow

private String userId;

private Result result; // successful or not

private String reason;

The problem I am facing is this: Some flows have only some of the parameters. For example, userId can be present in Purchasing flow but not in Search Flow. I had two attemps to solve the problem, however, the end code doesn't look like scalable and intuitive.

public class FlowLogDispatcher {
void publishSuccessfulPurchasingAttempt(String userId, long basketId) {
    FlowLogDto dto = FlowLogDto.builder()
. // similar methods follow

The end result turned out to be a huge amount of methods. Basically, moving the responsibility of creating FlowLogDto to the dispatcher.

I then tried to implement Strategy Pattern. I had two strategies, SuccessStrategy and FailureStrategy. Basically, since the amount of variables in each flow vary a lot, both Strategies looked fairly similar, except a single field (result). It even made the code much harder to read.

So, how do you go with consolidating all flows(with different values set) into a single end object (like FlowLog)? Like, how/what do you put on top of a Builder pattern?

ps: FlowLog has a builder pattern. It is also an option to build the object in each flow separately and send to s3 directly, which doesn't look scalable and readable to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.