java - Many-stepped procedure with several possibilities for errors- call each step separately from outer class, or have one method? -


newbie here.. i'm wondering best practice in following case:

i use mvc-pattern. controller-class needs call model-class perform procedure. procedure has 4 steps. in 3 first steps, if goes wrong, model produce list of strings, controller must ensure displayed user, in addition error message. in last step, model produce map, again controller must ensure showed user. in last step, timeout occur..

what best way handle this?

i have made 2 rough drafts of suggestions below.

alternative 1:

public class model{       public list<string> step1(){         // return empty list if ok, fill list otherwise     }      public list<string> step2(){    // return empty list if ok, fill list otherwise}      public map<string, string> step3(){     // return empty list if ok, fill list otherwise} }   public class controller{      model mymodel;       public void doprocedure(){          list<string> list = mymodel.step1();         if(list.size() != 0){             string errormessage = "step 1 error message"             // make sure view display list , errormessage             return;         }           list = mymodel.step2();         if(list.size() != 0){             string errormessage = "step 2 error message"             // make sure view display list , errormessage             return;         }          map<string, string> map = mymodel.step3();         if(map.size()!=0){             string errormessage = "step 3 error message"             // make sure view display map , errormessage             return;         }          // make view display "procedure ok" message user }    

what don't opens possibility controller forget step, or steps in wrong order.

alternative 2:

public class model {      final static int step1_error;     final static int step2_error;     final static int step3_error;      private void  step1() throws modelexception{         list<string> list;         if(somethingwentwrong){             throw new modelexception(step1_error, "errormessage step1", list)         }     }      private void step2() throws modelexception {.             list<string> list;         if(somethingwentwrong){             throw new modelexception(step2_error, "errormessage step2", list)         }     }      private void step3() throws modelexception{.         map<string, string> map;         if(somethingwentwrong){             throw new modelexception(step3_error, "errormessage step3", map)         }     }      public void procedure() throws modelexception{         step1();         step2();         step3();         }    }   public class controller{      model mymodel;      try{         model.procedure();     }     catch(modelexception e){         switch(e.geterrornum){             case // handle error type 1             case // handle error type 2 etc         }     }  }   public class modelexception extends exception{      list<string> list;     map<string, string> map;     int errornum;      public modelexception(int errornum, string message,  list<string>){         ....     }      public modelexception(int errornum, string message,  map<string><string>){         ....     } } 

the 2nd alternative better. can improve replacing errornum field proper hierarchy of exceptions (because each exception type should describe particular condition in execution):

public class modelexception extends exception{}  public class step1modelexception extends modelexception{     public step1modelexception(list<string> details) {...}     public list<string> getdetail(){...} }  public class step2modelexception extends modelexception{     public step2modelexception(list<string> details) {...}     public list<string> getdetail(){...} }  public class step3modelexception extends modelexception{     public step3modelexception(map<string,string> details) {...}     public map<string,string> getdetail(){...} } 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -