简体   繁体   English

我怎样才能改进这个逻辑?

[英]How can I improve this logic?

I have to implement following logic in java:我必须在java中实现以下逻辑:

  1. execute query Q1 on data D1 based on condition C1根据条件 C1 对数据 D1 执行查询 Q1
  2. if result of Q1 is more than zero then make another query Q2 on data D2 based on condition C2如果 Q1 的结果大于零,则根据条件 C2 对数据 D2 进行另一个查询 Q2
  3. If Q2 size is more than zero then make another query Q3 on data D2 based on condition C3如果 Q2 大小大于零,则根据条件 C3 对数据 D2 进行另一个查询 Q3
  4. If Q3 size is more than zero then make another query Q4 on data D2 based on condition C4如果 Q3 大小大于零,则根据条件 C4 对数据 D2 进行另一个查询 Q4

  5. Now if Q1 result size is zero then execute query Q5 on data D1 based on condition C5现在,如果 Q1 结果大小为零,则根据条件 C5 对数据 D1 执行查询 Q5

  6. If result of Q5 is more than zero then execute query Q6 on data D3 based on condition C2如果 Q5 的结果大于零,则根据条件 C2 对数据 D3 执行查询 Q6
  7. If result of Q6 is more than zero then execute query Q7 on data D3 based on condition C3如果 Q6 的结果大于零,则根据条件 C3 对数据 D3 执行查询 Q7
  8. If result of Q7 is more than zero then execute query Q8 on data D3 based on condition C4如果 Q7 的结果大于零,则根据条件 C4 对数据 D3 执行查询 Q8

So I'm thinking about to write the below logic:所以我正在考虑编写以下逻辑:

 R1 = getResult (dataset D1, condition C1); 
 //Query on data D1 based on  condition C1;
 if (R1 is not null)
 {
   R2  = getResult (dataset R1, condition C2);//  Query on data R1 based on condition C2;
   if(R2 is null)
   {
     R3 = getResult (dataset R1, condition C3);// Query on data R1 based on condition C3
     if(R3 is null)
     {
       R4 = getResult (dataset R1, condition C4);// Query on data R1 based on condition C4
       display(R4);
     }
     else
     {
       display(R3);
     }
   }
   else
   {
     display(R2);
   }
 }
 else
 { 
   R5 = getResult (dataset D1, condition C5);
   if(R5 is not null)
   {
     R6 = getResult (dataset R5, condition C2);//Query on data R5 based on condition C2;
     if(R6 is null)
     {
        R7 = getResult (dataset R5, condition C3);//Query on data R5 based on condition C3
        if(R7 is null)
        {
          R8 =  getResult(dataset R5, condition C4);//Query on data R5 based on condition C3
        }
        else
        {
          display(R8); 
        }
      }
      else
      {
        display(R6);
      }
    }
  }

Is there a better way to improve this logic?有没有更好的方法来改进这个逻辑?

No, it cannot be done better because you have this two dependency chains Q1 -> Q2 -> Q3 -> Q4 and Q5 -> Q6 -> Q7 -> Q8 which are connected by Q1 -> Q5, where A -> B means that B is executed if A fails.不,它不能做得更好,因为你有这两个依赖链 Q1 -> Q2 -> Q3 -> Q4 和 Q5 -> Q6 -> Q7 -> Q8 由 Q1 -> Q5 连接,其中 A -> B 表示如果 A 失败,则执行 B。 You have to go through this chains like you have implemented.你必须像你已经实施的那样经历这个链条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM