我必须输出一个基于一些条件的文本,我如何重构它以使理解和维护变得清晰?
如果最好的选项是用state替换,那么我需要为每个枚举组合创建一个类?
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();
}发布于 2015-09-27 17:53:43
这是一个非常好的例子,说明状态模式将有助于管理代码的复杂性,特别是在将来添加其他LicensingOption和CalcTypes时。
此模式用于计算机编程,根据同一对象的内部状态封装其变化的行为。对于对象来说,这是一种更干净的方式,可以在运行时更改其行为,而不必求助于大型的单块条件语句,从而提高可维护性。
下面我已经为您的案例附加了一个状态模式的实现示例,但是请注意,从代码示例中很难真正给您提供好的建议(printW1、printHeaderZ和printHeaderA实际上没有给我多少关于您的域的信息),但是我尽力给您提供了一些与您提供的代码相当的内容。您可能希望将某些行为从CalcType转移到LicesingOption,因为我不清楚这两种状态在应用程序中是如何相互交互的。
配置
// Whatever way you decide to get that configuration
public CalcType calcType = Config.CalcTypen
public LicensingOption license = Config.LicensingOptionCalcType类
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类
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++)。
https://stackoverflow.com/questions/32780211
复制相似问题