首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重构条件

重构条件
EN

Stack Overflow用户
提问于 2015-09-25 10:29:12
回答 1查看 153关注 0票数 1

我必须输出一个基于一些条件的文本,我如何重构它以使理解和维护变得清晰?

如果最好的选项是用state替换,那么我需要为每个枚举组合创建一个类?

代码语言:javascript
复制
public enum CalcType {A, B, C, D}
public enum LicensingOption {HOME, PRO, ULTIMATE}

public void printHeader() {
    switch (calc) {
        case A:
            printHeaderX();
            break;
        case B:
            printHeaderY();
            break;
        default:
            printHeaderByLicensingOption();
    }
}

public void printHeaderByLicensingOption() {
    switch (license) {
        case PRO:
            printHeaderW();
            break;
        case HOME:
            printHeaderZ();
            break;
        case ULTIMATE:
            printHeaderA();
            break;
    }
}

public void printFooter() {
    if (calc.equals(CalcType.A))
        printFooterX();
    else
        printFooterByLicensingOption();
}

public void printFooterByLicensingOption() {
    switch (license){
        case PRO:
            printFooterW();
            break;
        case HOME:
            printFooterZ();
            break;
        case ULTIMATE:
            printFooterA();
            break;
    }
}

public void printFooterW(){
    if (calc.equals(CalcType.B))
        printW1();
    else
        printW2();
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-27 17:53:43

这是一个非常好的例子,说明状态模式将有助于管理代码的复杂性,特别是在将来添加其他LicensingOption和CalcTypes时。

此模式用于计算机编程,根据同一对象的内部状态封装其变化的行为。对于对象来说,这是一种更干净的方式,可以在运行时更改其行为,而不必求助于大型的单块条件语句,从而提高可维护性。

下面我已经为您的案例附加了一个状态模式的实现示例,但是请注意,从代码示例中很难真正给您提供好的建议(printW1printHeaderZprintHeaderA实际上没有给我多少关于您的域的信息),但是我尽力给您提供了一些与您提供的代码相当的内容。您可能希望将某些行为从CalcType转移到LicesingOption,因为我不清楚这两种状态在应用程序中是如何相互交互的。

配置

代码语言:javascript
复制
// Whatever way you decide to get that configuration
public CalcType calcType = Config.CalcTypen 
public LicensingOption license = Config.LicensingOption

CalcType类

代码语言:javascript
复制
abstract class CalcType {
    public void printHeader() {
        // Default behavior of printHeader() is printHeaderByLicensingOption(); 
        //  except for CalcTypeA and CalcTypeB, so we are going to redefine them in CalcTypeA and CalcTypeB.
        license.printHeaderByLicensingOption()
    }

    public void printFooter() {
        // Default behavior of printFooter() is printFooterByLicensingOption(); 
        //  except for CalcTypeA, so we are going to redefine it in CalcTypeA.
        license.printFooterByLicensingOption()
    }

    public void printFooterW() {
        // Default behavior of printFooterW() is printW2(); 
        //  except for CalcTypeB, so we are going to redefine it in CalcTypeB.
        printW2();
    }
}

class CalcTypeA extends CalcType {
    public void printHeader() {
        printHeaderX()
    }

    public void printFooter() {
        printFooterX()
    }
}

class CalcTypeB extends CalcType {
    public void printHeader() {
        printHeaderY()
    }

    public void printFooterW() {
        printW1();
    }
}

LicensingOption类

代码语言:javascript
复制
abstract class LicensingOption {
    // Those methods all have very different behaviour and it's hard from you example to know if there is any "default". 
    // Assuming there is none, just declare them abstract and define them in subclasses only.
    abstract public void printHeaderByLicensingOption();
    abstract public void printFooterByLicensingOption();
}

class ProLicense {
    public void printHeaderByLicensingOption() {
        printHeaderW();
    }

    public void printFooterByLicensingOption() {
        calcType.printFooterW()
    }
}

class HomeLicense {
    public void printHeaderByLicensingOption() {
        printHeaderZ();
    }

    public void printFooterByLicensingOption() {
        printFooterZ()
    }
}

class UltimateLicense {
    public void printHeaderByLicensingOption() {
        printHeaderA();
    }

    public void printFooterByLicensingOption() {
        printFooterA()
    }
}

注意:确保声明的函数是虚拟的(默认情况下是C++,而不是C++)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32780211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档