I am building an extensible system which have following requirements:
- Each request will have 2 attributes: type, region
- Based on the values of type and region, different configs will be selected
- There are over 20+ classes which need to select configs based on type+region.
- Business logic involves calling 30+ functions in 20+ classes, so passing region and type is not possible. The business logic has multiple branches based on the product type and status. A master class contains the business logic and calls individual functions inside different classes to complete the business logic. Please refer to below code to get an idea about business logic.
One method to implement this is to create a class with two fields: type and region. At the entry point, create an object of this class and pass it along to every function call. Based on the type+region value from that passed object, select appropriate config.
Instead of passing type+region object in every function, is there any elegant solution? Like extending an object dynamically? Or set an object at entry point and then reuse it by extending any particular class. I am new to system design and inheritance, so I cannot think of an elegant solution.
E.g.: Following gives an overview of the business logic. There are over 20 Helpers in the MainLogic class and each of those helpers have 5+ data stores. I am looking for an elegant solution which doesn't involve passing region+type across all functions.
@AllArgsConstructor
class MainLogic {
BillingInfoHelper billingInfoHelper;
..........
public Boolean executeRequest(RequestInput requestInput){
final String region = requestInput.getRegion();
final String type = requestInput.getType();
final BillingInfo billingInfo = billingInfoHelper.get(region, type);
..............
}
}
@AllArgsConstructor
class BillingInfoHelper {
AccountIdDataStore accIdDataStore;
BankNameDataStore bankNameDataStore;
AddressDataStore addressDataStore;
........
public BillingInfo get(String region, String type){
String accountNumber = accIdDataStore.get(region, type);
String bankName = bankNameDataStore.get(region, type);
String address = addressDataStore.get(region, type);
return new BillingInfo(accountNumber, bankName, address);
}
}
@AllArgsConstructor
class AccountIdDataStore {
Config configStore
public String get(String region, String type) {
configStore.getAccountNumber(region, type)
}
}