<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    limq

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

    重構(gòu)—利用反射合并函數(shù)

        先看下重構(gòu)前的測試用例:


    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;
            }

        
        
    /**
         * 測試:登陸后讀取權(quán)限信息,并且封裝為樹形結(jié)構(gòu) 
         
    */

          
    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 閱讀(3077) 評論(1)  編輯  收藏

    評論

    # re: 重構(gòu)—利用反射合并函數(shù)  回復(fù)  更多評論   

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 久久最新免费视频| 亚洲免费福利在线视频| 国产精品国产亚洲区艳妇糸列短篇| 91禁漫免费进入| 日本成人在线免费观看| 亚洲二区在线视频| 亚色九九九全国免费视频| 亚洲午夜精品国产电影在线观看| **aaaaa毛片免费同男同女| 久久福利资源网站免费看| 国产精一品亚洲二区在线播放| www永久免费视频| 免费观看无遮挡www的视频| 亚洲视频在线观看| 日本高清在线免费| 亚洲综合激情五月丁香六月| 女人张开腿等男人桶免费视频| jizzjizz亚洲日本少妇| 四虎永久免费影院在线| www免费插插视频| 亚洲电影免费在线观看| 三年片在线观看免费大全| 亚洲精品乱码久久久久久V| 丁香亚洲综合五月天婷婷| h视频在线观看免费| 亚洲国产成人久久精品动漫| 91成年人免费视频| 亚洲综合一区二区国产精品| 中文字幕无码播放免费| 国产综合成人亚洲区| 亚洲精品无码永久中文字幕| 0588影视手机免费看片| mm1313亚洲国产精品无码试看| 亚洲乱色熟女一区二区三区丝袜| **毛片免费观看久久精品| 国产精品亚洲一区二区三区久久| 亚洲avav天堂av在线不卡| 午夜免费福利在线观看| 国产无遮挡又黄又爽免费网站| 亚洲免费二区三区| 国产亚洲精品不卡在线|