limq

          rainman
          隨筆 - 19, 文章 - 2, 評論 - 115, 引用 - 1
          數據加載中……

          重構—利用反射合并函數

              先看下重構前的測試用例:


          public class TestLogin extends TestCase{
              
          private ButtonManagerIbatis buttonManagerIbatis;
              
                
          protected  void setUp() throws Exception 
                      context 
          = getContext(); 
                      buttonManagerIbatis 
          = (ButtonManagerIbatis)context.getBean("buttonManagerIbatis");
                      
          super.setUp(); 
                  }
           
                  ApplicationContext context ;
                  
          protected ApplicationContext getContext() {    
                       String[] paths 
          = {"/context/application_context.xml"};
                       ApplicationContext   ctx 
          = new ClassPathXmlApplicationContext(paths);
                       
          return ctx;
                  }

              
              
          /**
               * 測試:登陸后讀取權限信息,并且封裝為樹形結構 
               
          */

                
          public void testLongin(){
                    List
          <Button> list = buttonManagerIbatis.getAuth("0000");
                    testall(list);
                    
                }

                  Map
          <String,Model> modelmap = new HashMap<String,Model>();
                  Map
          <String,Menu>  fmenumap = new HashMap<String,Menu>();
                  Map
          <String,Menu>  smenumap =  new HashMap<String,Menu>();
                  
                  
          public void testall(List<Button> buttonList){
                      
          for(Button button :buttonList){
                          test(modelmap,button);
                      }

                      
          for(Model model : modelmap.values()){
                          test2(smenumap,model);
                      }

                      
          for(Menu menu : smenumap.values()){
                          test3(fmenumap,menu);
                      }

                      
          for(Menu fmenu :fmenumap.values() ){
                          System.out.println(fmenu.getMenuName());
                          
          for(Menu smenu :fmenu.getMenus() ){
                              System.out.println(
          "  "+smenu.getMenuName());
                              
          for(Model model : smenu.getModels()){
                                  System.out.println(
          "    "+model.getName());
                                  
          for(Button b:model.getButtons()){
                                      System.out.println(
          "      "+ b.getButtonDesc());
                                  }

                              }

                          }

                      }

                  }


                  
          public void test(Map<String,Model> modelmap , Button b){
                      Model m 
          = b.getModel();
                      
          if(!modelmap.containsKey(m.getId()))
                          modelmap.put(m.getId(),m);
                      modelmap.get(m.getId()).getButtons().add(b);
                  }

                  
                  
          public void test2(Map<String,Menu> menumap , Model m){
                      Menu menu 
          = m.getMenu();
                      
          if(!menumap.containsKey(menu.getId()))
                          menumap.put(menu.getId(),menu);
                      menumap.get(menu.getId()).getModels().add(m);
                          
                  }

                  
                  
          public void test3(Map<String,Menu> menumap , Menu smenu){
                      Menu fmenu 
          = smenu.getMenu();
                      
          if(!menumap.containsKey(fmenu.getId()))
                          menumap.put(fmenu.getId(),fmenu);
                      menumap.get(fmenu.getId()).getMenus().add(smenu);
                  }

                  
          }

              這里有3個方法 test, test2 ,test3 考慮到以后還可能有變化,所以想把他們合成一個方法,首先考慮到了反射

                  public void testC(Map map , Object t , String parent ,String childrenname){
                      
          try {
                          Object t_parent 
          = BeanUtils.getDeclaredProperty(t, parent);
                          Object t_parent_id 
          = BeanUtils.getDeclaredProperty(t_parent, "id");
                          
          if(!map.containsKey(t_parent_id)){
                              map.put(t_parent_id, t_parent);
                          }

                          Object o 
          = map.get(t_parent_id);
                          Collection t_collection 
          =(Collection)BeanUtils.getDeclaredProperty(o, childrenname);
                          t_collection.add(t);
                      }
           catch (IllegalAccessException e) {
                          e.printStackTrace();
                      }
           catch (NoSuchFieldException e) {
                          e.printStackTrace();
                      }

                  }
          于是修改后的測試用例為:

          public class TestLogin extends TestCase{
              
          private ButtonManagerIbatis buttonManagerIbatis;
              
                
          protected  void setUp() throws Exception 
                      context 
          = getContext(); 
                      buttonManagerIbatis 
          = (ButtonManagerIbatis)context.getBean("buttonManagerIbatis");
                      
          super.setUp(); 
                  }
           
                  ApplicationContext context ;
                  
          protected ApplicationContext getContext() {    
                       String[] paths 
          = {"/context/application_context.xml"};
                       ApplicationContext   ctx 
          = new ClassPathXmlApplicationContext(paths);
                       
          return ctx;
                  }

                
          public void testLongin(){
                    List
          <Button> list = buttonManagerIbatis.getAuth("0000");
                    testall(list);
                    
                }

                    Map
          <String,Model> modelmap = new HashMap<String,Model>();
                    Map
          <String,Menu>  fmenumap = new HashMap<String,Menu>();
                    Map
          <String,Menu>  smenumap =  new HashMap<String,Menu>();
                  
                  
          public void testall(List<Button> buttonList){
                      
          for(Button button :buttonList){
                          testC(modelmap,button,
          "model","buttons");
                      }

                      
                      
          for(Model model : modelmap.values()){
                          testC(smenumap,model,
          "menu","models");
                      }

                      
          for(Menu menu : smenumap.values()){
                          testC(fmenumap,menu,
          "menu","menus");
                      }

                      
                      
          for(Menu fmenu :fmenumap.values() ){
                          System.out.println(fmenu.getMenuName());
                          
          for(Menu smenu :fmenu.getMenus() ){
                              System.out.println(
          "  "+smenu.getMenuName());
                              
          for(Model model : smenu.getModels()){
                                  System.out.println(
          "    "+model.getName());
                                  
          for(Button b:model.getButtons()){
                                      System.out.println(
          "      "+ b.getButtonDesc());
                                  }

                              }

                          }

                      }

                  }

                  
          /**
                   * 
                   * 
          @param map 
                   * 
          @param b
                   
          */

                  @SuppressWarnings(
          "unchecked")
                  
          public void testC(Map map , Object t , String parent ,String childrenname){
                      
          try {
                          Object t_parent 
          = BeanUtils.getDeclaredProperty(t, parent);
                          Object t_parent_id 
          = BeanUtils.getDeclaredProperty(t_parent, "id");
                          
          if(!map.containsKey(t_parent_id)){
                              map.put(t_parent_id, t_parent);
                          }

                          Object o 
          = map.get(t_parent_id);
                          Collection t_collection 
          =(Collection)BeanUtils.getDeclaredProperty(o, childrenname);
                          t_collection.add(t);
                      }
           catch (IllegalAccessException e) {
                          e.printStackTrace();
                      }
           catch (NoSuchFieldException e) {
                          e.printStackTrace();
                      }

                  }

                  
          }
          例外 BeanUtils中的 反射方法:
          static public Object getDeclaredProperty(Object object, String propertyName) throws IllegalAccessException, NoSuchFieldException {
                  Assert.notNull(object);
                  Assert.hasText(propertyName);
                  Field field 
          = object.getClass().getDeclaredField(propertyName);
                  
          return getDeclaredProperty(object, field);
              }

          posted on 2009-02-17 09:49 limq 閱讀(3078) 評論(1)  編輯  收藏

          評論

          # re: 重構—利用反射合并函數  回復  更多評論   

          反射在性能方面估計不是很滿意
          2009-07-05 18:35 | 冰河快狼

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 利津县| 栖霞市| 昂仁县| 衡山县| 敖汉旗| 大同县| 寿宁县| 蓬莱市| 昌邑市| 富裕县| 太谷县| 军事| 遂溪县| 上犹县| 永宁县| 南华县| 东海县| 丘北县| 黄山市| 宜君县| 呼和浩特市| 全椒县| 雅江县| 五大连池市| 萨嘎县| 铁岭县| 新余市| 嘉黎县| 霍林郭勒市| 张掖市| 兰坪| 澎湖县| 中宁县| 库尔勒市| 连山| 察隅县| 来凤县| 鸡西市| 花莲市| 城固县| 达州市|