帮助重构:引入参数对象?
我有一些相同的代码,除了特定的分配顺序以稍微不同的顺序发生之外。
很容易将其分解为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将委托解析输入
Class
或数组来简单地指示原始String
中项目的顺序以及它们应该如何分配,而不是传递Class
或数组。 code>line 到这个Class
。执行以下操作会更具可读性:最后,这不会太过分,3 个月后,当您返回此代码并发现它更容易理解时,您会感谢自己。
Instead of passing in a
Class
or array that simply indicates the ordering of the items in the rawString
and how they should be assigned, I would delegate the parsing of the inputline
to thisClass
. It would be much more readable to do the following: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.
恕我直言,最简单、最易读的方法是传递映射而不是 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.