6 Replies Latest reply: Mar 3, 2010 9:29 AM by 791266 RSS

    avoid if else using design pattern

    513500
      form.add(new AbstractSubmitComponentValidator(form, "finish"){
                     @Override
                     protected void customValidations(Form<?> form) {
                          StgAuditGeneral  stgAuditGeneral=stgAuditGeneralService.getById(stgAuditId);
                          for(EaAuditProgram  eaAuditProgram:stgAuditGeneral.getEaAuditPrograms()){
                               if(eaAuditProgram.getStgFindings().size()==0){
                                    addErrorMessage("Programs found with out findings");
                                    break;
                               }
                          }
                          for(StgFinding  stgFinding:stgAuditGeneral.getStgFindings()){
                               if(stgFinding.getEaAuditProgram()!=null&&stgFinding.getFindingType()==null){
                                    addErrorMessage("Finding Type not found for findings ");
                                    break;
                               }
                          }
                          for(StgFinding  stgFinding :stgAuditGeneral.getStgFindings()){
                               if(stgFinding.getEaAuditProgram()==null){
                                    addErrorMessage("Unassigned findngs exsists");
                                    break;
                               }
                          }
                          for(EaAuditProgram  eaAuditProgram:stgAuditGeneral.getEaAuditPrograms()){
                               if(eaAuditProgram.getInfUser()==null){
                                    addErrorMessage("Grants Officer not found  ");
                                    break;
                               }
                          }
                          if(stgAuditGeneral.getPrimaryGrant()==null){
                               addErrorMessage("No primary grant found");
                          }
                          for(EaAuditProgram  eaAuditProgram:stgAuditGeneral.getEaAuditPrograms()){
                               AuditProgramNotification  auditProgramNotification=eaAuditProgram.getAuditProgramNotification();
                               if(auditProgramNotification==null){
                                    addErrorMessage("No Notification sent");
                                    break;
                               }
                               if(auditProgramNotification.getAuditProgramNotificationStatus().equals(AuditProgramNotificationStatus.PENDING)){
                                    addErrorMessage("Notification  found with Pending status");
                                    break;
                               }
                          }
                     }
                });
      I am adding this kind of code in several places , I want to avoid this using some desing pattern , please suggest me design pattern to avoid code like above.
        • 1. Re: avoid if else using design pattern
          jduprez
          1) Visitor comes to mind.
          2) without making a full-fledged visitor, simply defining an interface Validator with multiple implementation classes, each implementation focusing on validating one member of the StdAuditGeneral. The client code could then have a list of instances of those validator classes, and iterate over the validators, passing them the StgAuditGeneral.
          • 2. Re: avoid if else using design pattern
            800387
            You can also avoid a lot of those if-else statements using [http://en.wikipedia.org/wiki/Strategy_pattern]

            - Saish
            • 3. Re: avoid if else using design pattern
              jschellSomeoneStoleMyAlias
              miro_connect wrote:
              I am adding this kind of code in several places ,
              Exactly the same code?
              • 4. Re: avoid if else using design pattern
                843853
                I definitely like the idea to use pattern get ride of if-else. But just want to say if-else has its own advantage. Most of all, if-else is simple, it put all logic in one place which helps you much easier to fix a bug or enhance (just add another line of if-else :D). The main problem of if-else is it's not extensible for outer code. But in many cases, the code is in a private place or let's say it's not a part of API, introduce pattern will just bring more complexity.

                I think you could review your code and if you find it's just an internal code for a product and no much extended work will be done (of course, no one could foresee this with 100%), I suggest you let it be. :) (Don't use pattern for pattern)

                Edited by: qqsu_azure on Mar 3, 2010 2:12 AM
                • 5. Re: avoid if else using design pattern
                  791266
                  qqsu_azure wrote:
                  I definitely like the idea to use pattern get ride of if-else. But just want to say if-else has its own advantage. Most of all, if-else is simple, it put all logic in one place
                  Huh? Not if you place them at many different locations (Like the OP said)
                  • 6. Re: avoid if else using design pattern
                    513500
                    Please tell me how strategy pattern can help me here , I saw the examples for implementing strategy pattern but I am not able to figure out how to implement this validations in strategy pattern ? Please help me with the code I posted.