本文介绍了在枚举结构上循环时重构和移除案例语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其自己的类中声明的枚举结构是业务逻辑类的成员变量。该枚举基本上代表了另一个类别的状态。

An enum structure declared in its own class is a member variable to the business logic class. That enum basically represents the state of that other class.

虽然我已经多次重新审视了这个问题,但是替换或者摆脱了这些案例陈述证明对我来说非常沮丧。

Although I have revisited the issue several times, replacing, or getting rid of those case statements proves quite frustrating to me.

几种业务逻辑方法简单地遍历枚举,并通过分配相同枚举的其他值和其他属性来更改该类的状态。

Several business logic methods simple iterate over the enum and change the state of that class by assigning another value of the same enum, and other properties.

public enum MyEnum{ A,B,C,D }

业务逻辑类有这个枚举作为成员:

The business logic class has this enum as a member:

public class BusinessLogic {

    private MyEnum CurrentSelection;
    private int propertyX;
    private int propertyY;

    public void operation1(){
        switch(CurrentSelection){
        case A: {alter propertyX this way; break;}
        case B: {alter propertyY this way; break;}
        case C: {alter propertyX that way; break;}
        case D: {alter propertyY that way; break;}
        }

    }

    public void operation2(){
        switch(CurrentSelection){
        case A: {CurrentSelection=MyEnum.B; break;}
        case B: {CurrentSelection=MyEnum.C; break;}
        ....etc
        }
    }

    public void operation3(){
        switch(CurrentSelection){
        case A: {CurrentSelection=MyEnum.D; break;}
        case B: {CurrentSelection=MyEnum.A; break;}
        ....etc
        }
    }
}

另一个客户端类将实例化业务逻辑类,初始化其属性,然后使用其操作方法。

Another client class will instantiate the business logic class, initialing its properties and then using its operation methods.

我已成功完成(借助于SO)将操作方法​​封装到一个命令模式结构中,所以我可以调用没有任何case语句的操作。 ()。

What I have successfully done (with help from SO) is encapsulate the operation methods into a command pattern structure so I can call the operations without any case statements. (here).

我想我的麻烦是如何在我的业务逻辑类中封装case语句。我怀疑我需要多态性和适当的数据结构。

I guess my trouble is how to encapsulate the case statements in my business logic class. I suspect that I would need polymorphism and the appropriate data structures.

重构专家建议每个case语句应该是一个通用接口的实现。但是如果我有3个方法遍历4个成员的枚举,这意味着我可能需要3个接口,每个4个实现,给我12个类(加3个接口)。这不会是一个班级超载吗?这些逻辑是正常的,这些方法是正常的,但问题是重复的switch / case语句。

The refactoring experts suggest each case statement should be an implementation of a common interface. But if I have 3 methods iterating over a 4-member enum that means I'd probably need 3 interfaces with 4 implementations each, giving me 12 classes (plus 3 interfaces). Wouldn't that be a class overload? The logic is working fine with those 3 methods as they are, but the issue are the repeating switch/case statements.

有没有办法重构这些switch语句,但是避免以多态为名的其他类无数的课程?是否可以确定迭代过程的一部分?或者只是案例(逻辑是)应该重构?

Is there a way to refactor those switch statements but avoid a myriad of other classes in the name of polymorphism? Would it be possible to factor out the iterate-over-the-enum part? Or just the cases (where the logic is) should be refactored?

作为第一步,我完全删除了这些方法让他们实现一个简单的界面:

As a first step I removed those methods completely my having them implement a simple interface:

public interface Command {
    void execute();
}

所以,它的操作方法实现了命令界面:

So, its operation method implemented the command interface:

public class Operation1 implements Command {

    private void doOperation1(){
        switch(CurrentSelection){
            ..all the cases here
        }
    }

    public void execute() {
        doOperation1();
    }

}

如我所见,购买我更清洁的业务逻辑类,但是交换机的路径会保持不变吗?

As I see this, it will buy my a cleaner business logic class, but the trail of the switch cases will remain, right?

推荐答案

你可以包括所有三个接口中的方法,并具有该接口的不同实现(每个枚举一个),并且该业务类委托调用..

you could include all three methods in an interface and have different implementations of that interface (one for each enum), and have this buisness class delegate the calls..

也用于更改状态,具有操作的返回值是该公共接口,因此在调用这些方法时,调用类可以返回下一个状态的适当对象。

also for changing the state, have the return value from the operation be that common interface so that upon invoking these methods, the invoking class could return the appropriate object for the next state.

这篇关于在枚举结构上循环时重构和移除案例语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:09