如何避免Java中的switch-case语句 [英] How to avoid switch-case statements in Java

查看:407
本文介绍了如何避免Java中的switch-case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TriggerType枚举,可以在其中添加不同的触发器

I have an enum of TriggerType, where different triggers can be added

public enum TriggerType {
    meta,data,list,toggle
}

这些触发器类型在不同的处理程序中使用(例如Component ,仪表板等),以识别通过开关盒在处理程序内部触发了哪个触发器,例如,下面给出了通过开关盒使用触发器的ComponentHandler的代码段

These trigger types are used inside different handlers (eg Component, Dashboard etc) to identify which trigger is triggered inside the handler through a switch-case, for eg Code snippet of ComponentHandler using trigger through switch-case is given below

@Override
public TriggerResultInterface executeTriggerJob(TriggerEventHelper triggerEventHelper) throws TriggerHandlerException {
    switch (triggerEventHelper.getTriggerName()) {
        case meta:
            return getMetaComponentConfig(triggerEventHelper);
        case data:
            return getComponentData(triggerEventHelper);
        default:
            LOGGER.debug(INVALID_TRIGGER_NAME_CONFIGURED);
            throw new TriggerHandlerException(INVALID_TRIGGER_NAME_CONFIGURED);
    }

}

想象一下,如果我想添加一个新的触发器,我必须更新不可避免的枚举类,与此同时,我必须更新需要使用该触发器的每个处理程序类,这种通过编码进行设计的方式是好的还是其他方法?更好的解决方案,它将增强此代码并遵循SOLID原则以及更好的设计。

Imagine If I want to add a new Trigger, I have to update the enum class which is unavoidable, at the same time I have to update each of my handler classes which that Trigger need to be used, Is this way of design with coding is good or is there any-other better solution that will enhance this code and follows SOLID principles along with a better design.

我想强调一点,这个问题不是。在这种情况下,每种类型仅需要一种行为(例如:convertToMp3)。但是我的问题是我的枚举类型(触发器类型)依赖于可能使用的处理程序,因此每个Trigger Type枚举的行为或实现将取决于所使用的处理程序的要求。

I would like to stress on saying that this question is not a duplicate of this. There is only one behavior needed in that situation for each type (eg: convertToMp3). But what my question refers to is my enum type (Trigger Type) is dependent on Handlers which it could possibly be used, so each Trigger Type enum's behavior or implementation will depend on the requirement of the handler it is being used.

推荐答案

解决方案之一是使用多态来不同地处理触发器。例如,您可以声明 Trigger 接口,并具有多个实现。在这种情况下,当您需要新的触发器类型时,只需实现此接口,而不用触摸现有代码:

One of the solution is to use polymorphism to handle triggers differently. For instance, you could declare the Trigger interface and have several implementations. In this case, when you need a new trigger type, you just implement this interface and don't touch the existing code:

public interface Trigger {
    TriggerResultInterface execute(TriggerEventHelper eventHelper);
}

public class MetaTrigger implements Trigger {
    @Override
    TriggerResultInterface execute(TriggerEventHelper eventHelper) {
        // do meta trigger work here
    }
}

public class DataTrigger implements Trigger {
    @Override
    TriggerResultInterface execute(TriggerEventHelper eventHelper) {
        // do data trigger work here
    }
}

// ...

public TriggerResultInterface executeTriggerJob(TriggerEventHelper eventHelper) {
    eventHelper.getTrigger().execute(eventHelper);
}

在这种情况下,将不可能添加新的触发器类型而不执行

In this case it will be impossible to add a new trigger type and not implement its behaviour.

如果需要默认实现,则可以使用基类代替接口(在Java 8中,可以在接口中添加默认实现)

If you need a default implementation, you can use a base class instead of the interface (in Java 8 you can add a default implementation right into the interface).

这篇关于如何避免Java中的switch-case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆