针对多种模式验证字符串的最佳方法
这是一个更多关于最佳实践/设计模式的问题,而不是关于正则表达式的问题。
简而言之,我有 3 个值:from、to 和我想要更改的值。 From 必须匹配多种模式之一:
XX.X
>XX.X
>=XX.X
<XX.X
<=XX.X
XX.X-XX.X
而 To 必须是十进制数。根据 From 中给出的值,我必须检查要更改的值是否满足 From 条件。例如,用户输入“从:>100.00到:150.00”意味着每个大于100.00的值都应该被改变。
正则表达式本身不是问题。问题是,如果我将整个 From 与一个正则表达式匹配并且通过,我仍然需要检查输入了哪个选项 - 这将在我的代码中生成至少 5 个 IF,每次我想添加另一个选项时,我都需要添加另一个选项如果 - 不酷。如果我要创建 5 个模式,也会发生同样的情况。
现在我有一个 HashMap,它保存一个模式作为键,一个 ValueMatcher 作为值。当用户输入 From 值时,我会在循环中将其与该映射中的每个键进行匹配,如果匹配,则我使用相应的 ValueMatcher 来实际检查我要更改的值是否满足“From”值。
另一方面,这种方法要求我拥有一个具有所有可能性的 HashMap、一个 ValueMatcher 接口和 5 个实现,每个实现只有 1 个短“匹配”方法。我认为它确实比 IF 更好,但看起来仍然是一个夸张的解决方案。
还有其他方法吗?或者这就是我实际上应该这样做的方式?我真的很遗憾我们不能在 HashMap 中保存方法/将它们作为参数传递,因为那样我就只有 1 个包含所有匹配方法的类并将它们存储在 HashMap 中。
This is a question more about best practices/design patterns than regexps.
In short I have 3 values: from, to and the value I want to change. From has to match one of several patterns:
XX.X
>XX.X
>=XX.X
<XX.X
<=XX.X
XX.X-XX.X
Whereas To has to be a decimal number. Depending on what value is given in From I have to check whether a value I want to change satisfies the From condition. For example the user inputs "From: >100.00 To: 150.00" means that every value greater than 100.00 should be changed.
The regexp itself isn't a problem. The thing is if I match the whole From against one regexp and it passes I still need to check which option was inputted - this will generate at least 5 IFs in my code and every time I want to add another option I will need to add another IF - not cool. Same thing if I were to create 5 Patterns.
Now I have a HashMap which holds a pattern as the key and a ValueMatcher as the value. When a user inputs a From value then I match it in a loop against every key in that map and if it matches then I use the corresponding ValueMatcher to actually check if the value that I want to change satisfies the "From" value.
This aproach on the other hand requires me to have a HashMap with all the possibilities, a ValueMatcher interface and 5 implementations each with only 1 short "matches" methode. I think it sure is better than the IFs, but still looks like an exaggerated solution.
Is there any other way to do it? Or is this how I actually should do it? I really regret that we can't hold methods in a HashMap/pass them as arguments because then I'd only have 1 class with all the matching methodes and store them in a HashMap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
责任链怎么样?
每个 ValueMatcher 对象恰好有一个 From/To 规则以及对链中下一个 ValueMatcher 的引用。每个 ValueMatcher 都有一个方法来检查候选者并对其进行转换或将其传递到链中的下一个。
通过这种方式添加新规则是一个简单的扩展,控制代码只是将候选者传递给链的第一个成员。
How about a chain of responsibility.
Each ValueMatcher object exactly one From/To rule and a reference to the next ValueMatcher in the chain. Each ValueMatcher has a method which examines a candidate and either transaforms it or passes it on to the next in the chain.
This way adding a new rule is a trivial extension and the controlling code just passes the candidate to the first member of the chain.
好吧,对于像根据运算符和限制值评估数字这样简单的事情,您难道不能编写一个稍微更通用的 ValueMatcher,它以限制值和运算符作为参数吗?然后,使用 >、>= 等的一些组合添加此 ValueMatcher 的 5 个实例将非常容易。
编辑:删除了非 Java 内容...对此感到抱歉。
Well, for something as simple as evaluating a number against an operator and a limit value, couldn't you just write one slightly more generic ValueMatcher which has a limit value and an operator as its parameters? It would then be pretty easy to add 5 instances of this ValueMatcher with a few combinations of >, >=, etc.
EDIT: Removed non Java stuff... sorry about that.