帮助重构:引入参数对象?

发布于 2024-08-06 08:01:17 字数 3022 浏览 3 评论 0原文

我有一些相同的代码,除了特定的分配顺序以稍微不同的顺序发生之外。

很容易将其分解为 int[] 类型的方法参数,它表示顺序。

然而,我发现这并不是最清楚的。

另一种选择是将它们分解为 AssignmentOrders 类型的对象。我还可以对对象构造函数中的值进行验证,而我无法对数组进行验证。这就是《重构》一书中的“引入参数对象”重构。

我想知道这种特殊的重构是否过度,我应该坚持使用 int[] 吗?

代码:

原始的三个示例:

private static PersonDetails parseLine(String line, String deliminator, int[] orderOfSections) 
            throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[0].trim();
        String value2 = sections[1].trim();
        String value3 = sections[4].trim();
        String value4 = sections[2].trim();
        String value5 = sections[3].trim();

        //........
    }

private static PersonDetails parseLine(String line, String deliminator) 
            throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[1].trim();
        String value2 = sections[0].trim();
        String value3 = sections[2].trim();
        String value4 = sections[3].trim();
        String value5 = sections[4].trim();

        //........
    }

private static PersonDetails parseLine(String line, String deliminator, int[] orderOfSections)


        throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[0].trim();
        String value2 = sections[1].trim();
        String value3 = sections[2].trim();
        String value4 = sections[4].trim();
        String value5 = sections[5].trim();

        //........
    }

我如何将上述 3 个重构为:

private static PersonDetails parseLine(String line, String deliminator, int[] orderOfSections) 
            throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[orderOfSections[0]].trim();
        String value2 = sections[orderOfSections[1]].trim();
        String value3 = sections[orderOfSections[2]].trim();
        String value4 = sections[orderOfSections[3]].trim();
        String value5 = sections[orderOfSections[4]].trim();

        //........
    }

理论上我如何将其重构为参数对象:

private static PersonDetails parseLine(String line, String deliminator, OrderOfSections order) 
        throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[order.getValue1Idx].trim();
        String value2 = sections[order.getValue2Idx].trim();
        String value3 = sections[order.getValue3Idx].trim();
        String value4 = sections[order.getValue4Idx].trim();
        String value5 = sections[order.getValue5Idx].trim();

        //........
    }

我想做的是创建一个特定的类,而不是使用 int[]...但想知道这是否太过分了。

好处是它会更具可读性。它可能不是 orderOfSections[0],而是 orderOfSections.value1SectionIdx...我还可以将一些验证代码放入类中。

我相信这就是 Martin Fowler 所说的引入参数对象。

编辑:

另一种选择是使用字典。比新类更轻量,但更具描述性...然后我可以使用类似 orderOfSections["value1"]

I have some code which is the same except that a certain sequence of assignments happen in slightly different orders.

It is easy to factor this out into a method parameter of type int[], which denotes the order.

However, I find it isn't the clearest.

Another option was to factor them out into a object of type AssignmentOrders. I can also do validation on the values in the object constructor that I wasn't able to do with the array. This would be the "Introduce Parameter Objects" refactoring from the book, Refactoring.

I am wondering if this particular refactoring is overkill and I should just stick with the int[]?

CODE:

Three Samples of the Originals:

private static PersonDetails parseLine(String line, String deliminator, int[] orderOfSections) 
            throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[0].trim();
        String value2 = sections[1].trim();
        String value3 = sections[4].trim();
        String value4 = sections[2].trim();
        String value5 = sections[3].trim();

        //........
    }

private static PersonDetails parseLine(String line, String deliminator) 
            throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[1].trim();
        String value2 = sections[0].trim();
        String value3 = sections[2].trim();
        String value4 = sections[3].trim();
        String value5 = sections[4].trim();

        //........
    }

private static PersonDetails parseLine(String line, String deliminator, int[] orderOfSections)


        throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[0].trim();
        String value2 = sections[1].trim();
        String value3 = sections[2].trim();
        String value4 = sections[4].trim();
        String value5 = sections[5].trim();

        //........
    }

How I've refactored the above 3 into this:

private static PersonDetails parseLine(String line, String deliminator, int[] orderOfSections) 
            throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[orderOfSections[0]].trim();
        String value2 = sections[orderOfSections[1]].trim();
        String value3 = sections[orderOfSections[2]].trim();
        String value4 = sections[orderOfSections[3]].trim();
        String value5 = sections[orderOfSections[4]].trim();

        //........
    }

How I could theoretically refactor it into a parameter object:

private static PersonDetails parseLine(String line, String deliminator, OrderOfSections order) 
        throws Exception {
        String[] sections = line.split(deliminator);

        String value1 = sections[order.getValue1Idx].trim();
        String value2 = sections[order.getValue2Idx].trim();
        String value3 = sections[order.getValue3Idx].trim();
        String value4 = sections[order.getValue4Idx].trim();
        String value5 = sections[order.getValue5Idx].trim();

        //........
    }

What I was thinking of doing was creating a specific class instead of using int[]... But wondered if that would be overkill.

The benefits are that it would be more readable. Instead of orderOfSections[0], it might be orderOfSections.value1SectionIdx... I could also put some validation code into the class.

I believe this is what Martin Fowler calls Introducing a Parameter Object.

EDIT:

Another option would be to use a Dictionary. Lighter-weight than a new class, but more descriptive... Then I could use something like orderOfSections["value1"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘸点软妹酱 2024-08-13 08:01:17

我将委托解析输入Class或数组来简单地指示原始String中项目的顺序以及它们应该如何分配,而不是传递Class或数组。 code>line 到这个Class。执行以下操作会更具可读性:

private static PersonDetails parseLine(String line, String deliminator, 
                         SectionsReader reader) throws Exception 
{
    reader.setLine(line);
    String value1 = reader.getValue1();
    String value2 = reader.getValue2();
    String value3 = reader.getValue3();
    String value4 = reader.getValue4();
    String value5 = reader.getValue5();

    //........
}

最后,这不会太过分,3 个月后,当您返回此代码并发现它更容易理解时,您会感谢自己。

Instead of passing in a Class or array that simply indicates the ordering of the items in the raw String and how they should be assigned, I would delegate the parsing of the input line to this Class. It would be much more readable to do the following:

private static PersonDetails parseLine(String line, String deliminator, 
                         SectionsReader reader) throws Exception 
{
    reader.setLine(line);
    String value1 = reader.getValue1();
    String value2 = reader.getValue2();
    String value3 = reader.getValue3();
    String value4 = reader.getValue4();
    String value5 = reader.getValue5();

    //........
}

In the end, this would not be overkill, and you will thank yourself in 3 months' time when you go back to this code and find it more intelligible.

王权女流氓 2024-08-13 08:01:17

恕我直言,最简单、最易读的方法是传递映射而不是 int 数组。

根据 PersonDetails 字段的外观,您甚至可以使用反射并在循环中分配值。

IMHO, the simplest and most readable way is to pass a map instead of an int array.

And depending on what your fields for PersonDetails look like, you could even use reflection and assign the values in a loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文