在Java中使代码更通用 [英] Make code more Generic in Java
问题描述
我有一个触发器管理器场景,其中我委托 触发器(以其他方式订阅触发器)到不同的处理程序。
现在我有三种处理程序类型,我使用开关案例与枚举(枚举这是处理程序类型)重定向到正确的处理程序。
但是我的代码似乎不可扩展,它不是通用的,它不遵循 SOLID 强>原则想象一下,如果我需要更多的处理程序,那么我将会最终来编辑我的交换机案例代码,并且我会有更多的案例来影响环路复杂度我的代码
以下是我的确切代码片段
private static TriggerContext getTriggerContext(TriggerHandlerType triggerHandlerType)throws TriggerHandlerException {
switch(triggerHandlerType){
case DASHBOARD_HANDLER:
triggerContext = new TriggerContext(new DashboardTriggerHandler());
return triggerContext;
case COMPONENT_HANDLER:
triggerContext = new TriggerContext(new ComponentTriggerHandler());
return triggerContext;
case WIDGET_HANDLER:
triggerContext = new TriggerContext(new WidgetTriggerHandler());
return triggerContext;
默认值:
LOGGER.error(MIS_CONFIGURED_REQUEST_IS_PROVIDED);
抛出新的TriggerHandlerException(TRIGGER_HANDLER_TYPE_GIVEN_IS_NOT_CONFIGURED_IN_THE_LIST_OF_TRIGGER_HANDLERS);
}
}
有人可以帮忙我加强了这个代码,我可以使它更通用,避免循环复杂,并遵循 SOLID 原则以及一些设计模式。
我想你的意思是使代码更加动态,而您的问题来自于将对象用作原语。
您的枚举对象应该包含要实例化的类型,而不是启用枚举对象:
枚举TriggerHandlerType {
DASHBOARD {
@Override
TriggerHandler create(){
return new DashboardTriggerHandler();
}
},
COMPONENT_HANDLER {
// ...
};
abstract TriggerHandler create();
}
getTriggerContext
调用 create()
来实例化处理程序:
private static TriggerContext getTriggerContext (TriggerHandlerType triggerHandlerType)throws TriggerHandlerException {
return new TriggerContext(triggerHandlerType.create());
}
I have a Trigger Manager scenario where I delegate the triggers (in other-words subscribe triggers) to different handlers.
For now I have three handler types, I use a switch-case with enum (enum here is the handler type) to redirect to correct handler.
But my code seems not extensible, its not generic and it doesn't follow SOLID principle. Imagine if I need to have more handler
I will be eventually coming and editing my switch case code and I will have more cases where it affects the cyclomatic complexity of my code
Below is my exact code snippet
private static TriggerContext getTriggerContext(TriggerHandlerType triggerHandlerType) throws TriggerHandlerException {
switch (triggerHandlerType) {
case DASHBOARD_HANDLER:
triggerContext = new TriggerContext(new DashboardTriggerHandler());
return triggerContext;
case COMPONENT_HANDLER:
triggerContext = new TriggerContext(new ComponentTriggerHandler());
return triggerContext;
case WIDGET_HANDLER:
triggerContext = new TriggerContext(new WidgetTriggerHandler());
return triggerContext;
default:
LOGGER.error(MIS_CONFIGURED_REQUEST_IS_PROVIDED);
throw new TriggerHandlerException(TRIGGER_HANDLER_TYPE_GIVEN_IS_NOT_CONFIGURED_IN_THE_LIST_OF_TRIGGER_HANDLERS);
}
}
Can someone help me to enhance this code in-which I can make it more generic and avoid cyclomatic complexity and follow SOLID Principle along with some design pattern.
I think you mean "make code more dynamic", and your problem comes from using objects as primitives.
Rather than switching on the enum object, your enum objects should contain the type to be instantiated:
enum TriggerHandlerType {
DASHBOARD {
@Override
TriggerHandler create() {
return new DashboardTriggerHandler();
}
},
COMPONENT_HANDLER {
//...
};
abstract TriggerHandler create();
}
getTriggerContext
can then call create()
to instantiate the handler:
private static TriggerContext getTriggerContext(TriggerHandlerType triggerHandlerType) throws TriggerHandlerException {
return new TriggerContext(triggerHandlerType.create());
}
这篇关于在Java中使代码更通用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!