在Java中处理n个if-else的更好方法 [英] Better way to handle n number of if-else if in java

查看:89
本文介绍了在Java中处理n个if-else的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有更好的方法来处理n。



我有一种情况需要基于n no打印不同的值。 if / else-if / conditions块之类的

  if(p == 1&& q == r)
System.out.println(条件1);
else if(r == p&& q == 9)
System.out.println(条件2);
else if(z == 1&&s == r)
System.out.println(条件3);
else if(p == 1 || x == r& y == 7)
System.out.println(条件4);
else if(q == z& y == r || p == 4)
System.out.println(条件5);
else if(x == z& r == 5 || z == 30)
System.out.println(条件6);
else if(s == 1 || q == x)
System.out.println(条件7);
else if(r == 14 || q == r ++ || z == y)
System.out.println(条件8);
else if(q == 18& s == r || p == 90)
System.out.println(条件9);
else if(y == 19 || q == 89)
System.out.println(条件10);
...

还有其他方法来处理它,而不是放置多个if / else if语句,以便以后出现任何新情况时,将很容易处理它。

解决方案

此处的真实答案



在良好的OO设计中,您可以通过非常不同的方式解决此问题。例如通过使用多态性。您实际上是想避免拥有许多不同的信息来源,然后做出决定。



例如,我会考虑使用FSM和状态模式



我的意思是:您的代码显示出解决问题的某种方法。只要您保持这种方法不变,您只是在谈论寻找以最少丑陋的方式表达您的解决方案的方法。



相反,您应该退后一步,研究可以用真正美丽的方式表达的方法。





此处的最小操作:您可以从声明一个枚举开始,例如:

  public枚举条件{
A,B,C,...

那么您将拥有一种隐藏所有这些语句的工厂方法;例如:

 条件currentCondition =确定条件(p,q,r,z); 
switch(currentCondition){
案例A:

含义:您要至少将这些知识集中到某个地方,以避免代码重复。但是又就像在旧的生锈的汽车上放一些新颜色一样。暂时有用,但并没有真正改善。



关于您的评论:是的,这种切换仅比其他方式稍好。但是正如您所说的:您不可能有太多的机会,因此至少您想要精确地确定一个确定状态的代码。



但是最后:我认为您在错误的层面上解决了这个问题。您可能正在处理某种复杂的业务逻辑;并且您想像这样从低层次解决问题。但是,您在这种级别上所做的任何事情都不会导致强大,长期可维护的解决方案。真正的解决方案可能是退后一步,然后使用某种工作流引擎进行研究。 p>

I was wondering if there is any better way to handle n no. of if/else-if block in Java.

I have a situation where I need to print different values based on n no. of if/else-if/conditions blocks like

if(p==1 && q==r)
  System.out.println("Condition 1");
else if(r==p && q==9)
  System.out.println("Condition 2");
else if(z==1 && s==r)
  System.out.println("Condition 3");
else if(p==1 || x==r && y==7)
  System.out.println("Condition 4");
else if(q==z && y==r || p==4)
  System.out.println("Condition 5");
else if(x==z && r==5 || z==30)
  System.out.println("Condition 6");
else if(s==1 || q==x)
  System.out.println("Condition 7");
else if(r==14 || q==r++ || z==y)
  System.out.println("Condition 8");
else if(q==18 && s==r || p==90)
  System.out.println("Condition 9");
else if(y==19 || q==89)
  System.out.println("Condition 10");
...

Is there any other way to handle it instead of putting multiple if/else if statements so that if later on any new condition come, it will be easy to handle it.

解决方案

The real answer here: don't do that.

In good OO design, you solve this problem in very different ways; for example by using polymorphism. You actually want to avoid having many different sources of information, to then make decisions on that.

In your case; I would be thinking towards FSMs and the state pattern for example.

What I mean is: your code is showing a certain "approach" towards solving a problem. And as long as you keep that approach as is, you are only talking about finding ways to express your solution in the "least ugly" way.

Instead, you should step back and look into approaches that can be expressed in "really beautiful" ways.

Thus: there is no "simple" direct answer to your question. You have to step back and have a close look at the requirements you want to fulfill; to then design a better, "more OO" solution to that.

The minimal thing here: you could start by declaring an enum like:

public enum Condition {
  A, B, C, ...

and then you would have some factory method that hides all those statements; like:

Condition currentCondition = determineCondition(p, q, r, z);
switch(currentCondition) {
  case A:

Meaning: you want to at least "centralize" that knowledge somewhere to avoid code duplication. But again; that is just like putting some new color on an old, rusty car. It helps for the moment, but doesn't really improve things.

Regarding your comments: yes, the switch is only marginally "better" than if elses. But as you said: you can't make too many chances, so at least you want to have exactly one piece of code that determines state.

But in the end: I think you are approaching this on a wrong level. You are probably dealing with some kind of complicated business logic; and you want to solve that on a low level like this. But nothing you do on such a level will lead to a robust, long-term maintainable solution. Probably the real solution is to step back and look into using some kind of workflow engine.

这篇关于在Java中处理n个if-else的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆