考虑以下代码:
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 个匹配器,所有匹配器都是独立的(如果一个是真的,它不会说明其他匹配器......),对于每个为真的匹配器 - 我正在对其匹配的内容调用不同的方法。
问题:我不喜欢这里的代码重复和“幻数”,但我想知道是否有更好的方法......?(也许是访客模式?)有什么建议吗?