<分区>
我正在构建一个具有以下要求的可扩展系统:
- 每个请求都会有2个属性:type, region
- 根据type和region的值,选择不同的配置
- 有超过20+类需要根据类型+地区选择配置。
- 业务逻辑涉及调用 20 多个类中的 30 多个函数,因此无法传递区域和类型。业务逻辑根据产品类型和状态有多个分支。主类包含业务逻辑并调用不同类中的各个函数来完成业务逻辑。请引用以下代码以了解业务逻辑。
实现它的一种方法是创建一个具有两个字段的类:type 和 region。在入口点,创建此类的对象并将其传递给每个函数调用。根据传递对象的类型+区域值,选择适当的配置。
不是在每个函数中传递类型+区域对象,有什么优雅的解决方案吗?喜欢动态扩展对象?或者在入口点设置一个对象,然后通过扩展任何特定类来重用它。我是系统设计和继承的新手,所以我想不出一个优雅的解决方案。
例如: 下面给出了业务逻辑的概述。 MainLogic 类中有 20 多个助手,每个助手都有 5 个以上的数据存储。我正在寻找一种优雅的解决方案,它不涉及在所有函数中传递 region+type。
@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)
}
}