I am currently working on a Spring Boot 3 application with Kotlin and Gradle, and I've encountered a challenge that I think many of you might find interesting. My application deals with complex business logic that involves numerous conditional operations. The challenge is to implement this logic in a way that is both efficient and maintainable.
Here's a simplified version of what I am trying to achieve:
Here are some specific questions:
I use the "when" construct, which makes it easier to write conditions, but also becomes cumbersome when you need to check a lot of nested conditions.
It is hard to say without code what pattern should be used. However, these words remind me that Strategy pattern can be used:
Each set of conditions can lead to different outcomes or actions
You put your conditions in some method and then when decision is made about what strategy should be used, you can apply it.
I am sorry I do not know Kotlin, so let me show an example via C#.
This is the method which has complex logic like you have
public DataProviderType GetDataProviderType()
{
if (...)
return DataProviderType.Csv;
return DataProviderType.Api
}
These are enums which corresponds to the strategy:
public enum DataProviderType
{
RabbitMq, Api, Csv
}
And this is a factory of strategies:
public class DataProviderFactory
{
private Dictionary<DataProviderType, IDataProvider> _dataProviderByType
= new Dictionary<DataProviderType, IDataProvider>()
{
{ DataProviderType.RabbitMq, new RabbitMQDataProvider() },
{ DataProviderType.Api, new ApiDataProvider() },
{ DataProviderType.Csv, new CsvDataProvider() },
};
public IDataProvider GetInstanceByType(DataProviderType dataProviderType) =>
_dataProviderByType[dataProviderType];
}
And then you can get your strategy like this:
DataProviderType dataProviderType = GetDataProviderType();
DataProviderFactory dataProviderFactory = new();
IDataProvider dataProvider = dataProviderFactory
.GetInstanceByType(DataProviderType.Api);
string data = dataProvider.Get();