处理 (2 raise to n) -1 条件
我有一个逻辑问题。我有员工对象的集合 有3个过滤条件,有句柄 例如员工姓名、办公室名称、工资。
现在这些过滤条件应该匹配(员工姓名和/或办公室名称和/或薪水),
所以在这里我必须编写 (2 raise n) -1 if 条件来处理这种情况。
我们还有其他方法可以做到这一点吗? 对于(员工姓名和/或办公室名称)条件,我正在执行以下操作
if (criteria.EmpName != "" && criteria.OfficeName != "")
{
if (emp.EmpName == criteria.EmpName && emp.OfficeName == criteria.OfficeName)
{
bIsMatch = true;
}
}
else
{
if (criteria.EmpName != "" && emp.EmpName == criteria.EmpName)
bIsMatch = true;
else if (criteria.OfficeName != "" && emp.OfficeName == criteria.OfficeName)
bIsMatch = true;
}
,现在如果必须处理萨拉利,我也写了至少 5 个条件。
还有其他方法吗?
I have one logical question. I have collection of employee objects
There are 3 filter criteria conditions which have handle
For e.g. Employee name, Office name, salary.
Now these filter criteria should match like (Employee name AND/OR Office name AND/OR salary)
So here I have to write (2 raise n) -1 if conditions to handle this situation.
Is there any other way we can do this.
For (Employee name AND/OR Office name) condition I m doing following
if (criteria.EmpName != "" && criteria.OfficeName != "")
{
if (emp.EmpName == criteria.EmpName && emp.OfficeName == criteria.OfficeName)
{
bIsMatch = true;
}
}
else
{
if (criteria.EmpName != "" && emp.EmpName == criteria.EmpName)
bIsMatch = true;
else if (criteria.OfficeName != "" && emp.OfficeName == criteria.OfficeName)
bIsMatch = true;
}
Now if have to handle saraly also i have write min 5 conditions.
Is thr other way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有很多方法可以做到这一点,但是由于您没有指定一种特定语言,并且我觉得没有资格判断您的编码风格,因此这里有一种保留代码的一般形式,同时演示一些更好的逻辑的方法:
There are lots of ways to do it, but since you didn't specify one specific language and since I don't feel qualified to judge your coding style, here's one that keeps the general form of your code, while demonstrating some better logic:
您可以配对过滤条件并使用一个语句对所有参数进行编码:
在每个 AND 表达式中,首先检查过滤器是否为空,如果是,则该部分将返回
true
,如果不是,则针对emp
中的相应值执行检查,并且仅当该检查为true
时才为true
。You can pair up your filtering conditions and have a single statement that encodes all the parameters:
In each of the AND-ed expressions checks first if the filter is empty, if it is that piece will result in
true
, if it's not, then the check is performed against the corresponding value inemp
and istrue
only when that check istrue
.首先假设您有一场比赛,然后一一应用每个标准。
或者,编写一个执行匹配的辅助函数。
然后你可以在一个大的 if 语句中完成所有操作:
Start out by assuming you have a match and Then apply each criterion one by one.
Or, write a helper function that does the matching.
Then you can do everything in one big if statement:
您可以单独检查条件并维护匹配的计数。这样你只需要
n
个条件:You can check the criteria individually and maintain a count of matches. That way you need only
n
conditions:这个怎么样?这个想法可以很好地适应更多的过滤器,除了映射本身是基于约定的(名称 - 名称)。
不过,我会对整体设计提出质疑;事情似乎有些不对劲。您将如何处理您提到的
Salary
字段?当然,这不是一个字符串
?在这种情况下使用的哨兵值是什么?How about this? The idea scales well for more filters, except that the mapping itself is convention based (name - name).
I would question the overall design though; there's something that doesn't seem right about it. How would you deal with the
Salary
field that you mention? Surely, that's not astring
? What's the sentinel-value being used in that case?在编写代码之前,请确保您对业务逻辑足够清晰。根据您的代码,我可以看到您想要检查
emp
和criteria
是否具有相同的EmployeeName
和OfficeName
>,如果任何属性是string.Empty
,则被认为是相同的。自己清楚之后代码就相当清晰了。开始了:Make sure you are clear enough about the business logic before writing the code. According to your code, I can see that you want to check if
emp
andcriteria
have the sameEmployeeName
andOfficeName
, any of the properties is considered to be the same if it'sstring.Empty
. The code will be quite clear after yourself is clear. Here we go:单独测试每个问题并使用位集对答案组合进行编码。
这会产生更干净的代码,因为您只需测试每个条件一次,它紧凑且可读,而且您可以轻松插入代码来处理每个组合。而且速度也很快。 O(n) 测试所有标准,O(1) 找到实际组合。
对于少量固定数量的条件,您可以手动推送位。对于许多标准或可扩展的解决方案,请使用 java.util.BitSet
位推送示例:
您可以使用 java.util.BitSet 来概括此解决方案以操作 n 个标准的位(当 n > 64 时有用!)。为了便于快速查找,请将每个 BitSet 组合的哈希存储在将哈希代码映射到命令类的映射中。
Test each question individually and use a bit set to encode the combinations of answers.
This results in cleaner code because you only test each criteria once, it's compact yet readable, and yet you can easily plug in code to handle each combination. And it's the fast too. O(n) to test all the criteria and O(1) to find the actual combination.
For a small, fixed number of criteria, you can push bits around manually. For many criteria, or for a solution that scales, use java.util.BitSet
Bit pushing example:
You can generalize this solution using java.util.BitSet to manipulate bits for n criteria (useful when n > 64!). To facilitate quick look up, store the hash of each BitSet combination in a map that maps the hash code to a command class.