如果是其他逻辑,有什么更好的方法来实现它?

时间:2014-10-30 23:12:34

标签: java

我有一个问题,让我的逻辑工作或更好仍然我需要一个更好的方法来实现这种if-else情况。

我有3个不同的条件设置不同的属性,我希望数据只有在所有条件评估为真时才持久保存到数据库,并且当任何或所有不同条件评估为false时不保持数据。

if (a>b){
  em.setAlpha();
}else{
  //throw error that prevents em.persist
}

if (c>d){
  em.setBeta();
}else{
  //throw error that prevents em.persist
}

if (e>f){
  em.setOmega();
}else{
  //throw error that prevents em.persist
}

//Should persist only when all conditions evaluate to true and not 
//persist when one or more of the conditions evaluates to false(throws an error)
database.persist(em);

4 个答案:

答案 0 :(得分:1)

使用and operator

会更好

注意:我不完全确定你的setAlpha函数是什么,所以这个可能不起作用。如果您在原始问题中澄清这一点,我将在此处解决。

if (a > b && c > d && e > f) {
    em.setAlpha();
    em.setBeta();
    em.setOmega();
    database.persist(em);
}
else {
    // We show up here if one of the conditions was false.
}

希望这有帮助。祝你好运!

答案 1 :(得分:1)

使用布尔标志:

boolean persist = true;

if (a>b){
  em.setAlpha();
}else{
  persist = false;
}

if (c>d){
  em.setBeta();
}else{
  persist = false;
}

if (e>f){
  em.setOmega();
}else{
  persist = false;
}

if (persist) {
  database.persist(em);
}

答案 2 :(得分:1)

如果处理订单已修复,您可以使用OOP设计模式"责任链"对于这个问题。这是一个小程序,我把它放在一起作为例子:

class JavaObj  {
    public void setAlpha() {}
    public void setBeta() {}
    public void setOmega() {};
}
class DB  {
    public void persist(JavaObj obj) {}
}
abstract class Handler {
    protected Handler successor;
    public void setSuccessor(Handler successor){
        this.successor = successor;
    }
    abstract public boolean processRequest(Integer [] params, JavaObj request);
}
class AlphaHandler extends Handler {
    public AlphaHandler (Handler successor) { setSuccessor(successor); }
    public boolean processRequest(Integer [] params, JavaObj request) {
        if (params[0]>params[1]) {
            request.setAlpha();
            return (successor==null) ? true : successor.processRequest(params, request);
        } else {
            return false;
        }
    }
}
class BetaHandler extends Handler {
    public BetaHandler (Handler successor) { setSuccessor(successor); }
    public boolean processRequest(Integer [] params, JavaObj request) {
        if (params[2]>params[3]) {
            request.setBeta();
            return (successor==null) ? true : successor.processRequest(params, request);
        } else {
            return false;
        }
    }
}
class OmegaHandler extends Handler {
    public OmegaHandler (Handler successor) { setSuccessor(successor); }
    public boolean processRequest(Integer [] params, JavaObj request) {
        if (params[4]>params[5]) {
            request.setOmega();
            return (successor==null) ? true : successor.processRequest(params, request);
        } else {
            return false;
        }
    }
}
public class ChainOfResponsibilityAsExample {
    // setup the chain of responsibilities 
    private static Handler omegaHandler = new OmegaHandler(null);
    private static Handler betaHandler = new BetaHandler(omegaHandler);
    private static Handler processor = new AlphaHandler(betaHandler);

    public static void main(String[] args) {
         DB database = new DB();
         JavaObj em = new JavaObj();
         Integer[] intArray = new Integer[] { 0, 1, 2, 3, 4, 5 };

         if (processor.processRequest(intArray, em)) {
             database.persist(em);
         }
     }
}

答案 3 :(得分:-1)

你可以这样做,使用和缩短你的代码:

// We set the valid ones
if (a > b) {
    em.setAlpha();
} else {
    //throw error that prevents em.persist
}
if (c > d) {
    em.setBeta();
} else {
    //throw error that prevents em.persist
}
if (e > f) {
    em.setOmega();
} else {
    //throw error that prevents em.persist
}

public boolean validate() {
    if ((a > b) && (c > d) && (e > f)) {
        return true;
    } else {
        return false;
    }
}

if (validate()) {
    database.persist(em);
}

之后,您可以致电if validate()以更快地检查条件。