如果使用JAVA,如何编写更优化和通用的代替200?

问题描述 投票:0回答:1

我正在处理报告代码,需要生成此报告输出。

汽车对象具有50个字符串,50个整数,50个日期,50个大十进制字段。其中一些值已满。

如果要寻找更优化和通用的版本,我通过输入200取消了这些控件。

if (carDTO.getCarProperty() != null) {
    if (carContent.getVariableType().equals(VariableType.STRING)) {
        if (carDTO.getCustomEntryString1() != null) {
            ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryString1());
        } else if (carDTO.getCustomEntryString2() != null) {
            ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryString2());
        } else if (carDTO.getCustomEntryString3() != null) {
            ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryString3());
        }

        ...

         else if (carDTO.getCustomEntryString50() != null) {
            ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryString50());
        }
    } else if (carContent.getVariableType().equals(VariableType.BIGDECIMAL)) {
        if (carDTO.getCustomEntryBigDecimal1() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryBigDecimal1());
        } else if (carDTO.getCustomEntryBigDecimal2() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryBigDecimal2());
        } else if (carDTO.getCustomEntryBigDecimal3() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryBigDecimal3());
        }

        ...

         else if (carDTO.getCustomEntryBigDecimal50() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryBigDecimal50());
        }

    } else if (carContent.getVariableType().equals(VariableType.INTEGER)) {
         if (carDTO.getCustomEntryInteger1() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryInteger1());
        } else if (carDTO.getCustomEntryInteger2() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryInteger2());
        } else if (carDTO.getCustomEntryInteger3() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryInteger3());
        }

        ...

         else if (carDTO.getCustomEntryInteger50() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryInteger50());
        }

    } else if (carContent.getVariableType().equals(VariableType.ZONEDDATETIME)) {
        if (carDTO.getCustomEntryDate1() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryDate1());
        } else if (carDTO.getCustomEntryDate2() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryDate2());
        } else if (carDTO.getCustomEntryDate3() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryDate3());
        }

        ...

         else if (carDTO.getCustomEntryDate50() != null) {
             ReportUtils.setValue(worksheet, rowIndex, cellIndex, carDTO.getCustomEntryDate50());
        } 
    }

}

如果要寻找更优化和通用的版本,我通过输入200取消了这些控件。

例如;

carDTO.getCustomEntryString1()=“ test1”,

carDTO.getCustomEntryString2()=“ test2”,

carDTO.getCustomEntryString3()=“ test3”

字段carDTO.getCustomEntryString1(),carDTO.getCustomEntryString2(),carDTO.getCustomEntryString3()已满,但是如果在我为报告编写的代码中输入了第一个字段,它将保留条件并填充相同的数据。写入值“ test1”的“ carDTO.getCustomEntryString1()”。

|-----------------------|----------------------|-----------------------|
| getCustomEntryString1 |getCustomEntryString2 | getCustomEntryString3 |
|-----------------------|----------------------|-----------------------|
|       test1           |         test1        |    test1              |
|-----------------------|----------------------|-----------------------|

如何解决此问题并编写更优化的版本?

如何编写更优化和通用的代替200,如果是?

java if-statement refactoring javabeans
1个回答
0
投票

老实说,我无法跟踪所有这些条件来回答问题的“此实现有什么问题”部分。但是,在架构方面,我认为拥有200种getter方法是一个可怕的想法。而是考虑使用HashMap并使用属性名称检索这些字段。想象一下像>>这样的签名

String getStrProperty(String name)
Integer getIntProperty(String name)
Date getDateProperty(String name)
BigInteger getBIProperty(String name)

您还希望有四个环绕HashMap.put(Key, Val)的二传手

edit:Nit-picky,但您不想要更多的generic

实现。您需要更多的[[abstract实现。除非您想销毁不同类型的属性之间的类型检查并将它们全部转换为Object,否则泛型实际上不适合在这里使用。
© www.soinside.com 2019 - 2024. All rights reserved.