避免代码重复
考虑以下代码:
if (matcher1.find()) {
String str = line.substring(matcher1.start()+7,matcher1.end()-1);
/*+7 and -1 indicate the prefix and suffix of the matcher... */
method1(str);
}
if (matcher2.find()) {
String str = line.substring(matcher2.start()+8,matcher2.end()-1);
method2(str);
}
...
我有 n 个匹配器,所有匹配器都是独立的(如果一个匹配器为真,则它对其他匹配器一无所知......),对于每个为真的匹配器 - 我对它匹配的内容调用不同的方法。
问题:我不喜欢这里的代码重复和“幻数”,但我想知道是否有更好的方法来做到这一点......? (也许是访客模式?)有什么建议吗?
consider the following code:
if (matcher1.find()) {
String str = line.substring(matcher1.start()+7,matcher1.end()-1);
/*+7 and -1 indicate the prefix and suffix of the matcher... */
method1(str);
}
if (matcher2.find()) {
String str = line.substring(matcher2.start()+8,matcher2.end()-1);
method2(str);
}
...
I have n matchers, all matchers are independent (if one is true, it says nothing about the others...), for each matcher which is true - I am invoking a different method on the content it matched.
question: I do not like the code duplication nor the "magic numbers" in here, but I'm wondering if there is better way to do it...? (maybe Visitor Pattern?) any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建一个抽象类,并在子类中添加偏移量(也可以进行字符串处理......根据您的要求)。
然后将它们填充到列表中并处理该列表。
这是一个示例抽象处理器:
Create an abstract class, and add offset in subclass (with string processing too... depending of your requirement).
Then populate them in a list and process the list.
Here is a sample absract processor:
只需使用捕获组标记要传递给方法的正则表达式部分。
例如,如果您的正则表达式是
foo.*bar
并且您对foo
或bar
不感兴趣,请将正则表达式设置为foo(. *)栏
。然后始终从Matcher
中获取组 1。然后,您的代码将如下所示:
进一步的一步是将您的方法替换为实现如下所示的类:
然后您可以轻松地自动化该任务:
请注意,如果性能很重要,则预编译
模式 可以提高运行时间。
Simple mark the part of the regex that you want to pass to the method with a capturing group.
For example if your regex is
foo.*bar
and you are not interested infoo
orbar
, make the regexfoo(.*)bar
. Then always grab the group 1 from theMatcher
.Your code would then look like this:
One further step would be to replace your methods with classes implementing an like this:
Then you can easily automate the task:
Note that if performance is important, then pre-compiling the
Pattern
can improve runtime if you apply this to many inputs.你可以把它缩短一点,但我的问题是,这真的值得付出努力吗:
You could make it a little bit shorter, but I the question is, is this really worth the effort:
将 Cochard 的解决方案与工厂(switch 语句)与所有 methodX 方法结合使用。所以你可以这样称呼它:
你可以在 Cochard 解决方案的填充步骤中分配 myEnum.MethodX
use Cochard's solution combined with a factory (switch statement) with all the methodX methods. so you can call it like this:
you can assign the myEnum.MethodX in the population step of Cochard's solution