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

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

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

    limq

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

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

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


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

        
        
    /**
         * 測(cè)試:登陸后讀取權(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個(gè)方法 test, test2 ,test3 考慮到以后還可能有變化,所以想把他們合成一個(gè)方法,首先考慮到了反射

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

            }
    于是修改后的測(cè)試用例為:

    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) 評(píng)論(1)  編輯  收藏

    評(píng)論

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

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

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 日韩免费视频播播| 久久久久久久99精品免费观看| 99热在线精品免费全部my| 亚洲AV美女一区二区三区| a毛片免费在线观看| 亚洲日韩精品一区二区三区| 毛片基地看看成人免费| 亚洲色大成网站WWW久久九九| 一级特黄录像视频免费| 亚洲美女高清一区二区三区| 一本久久A久久免费精品不卡| 亚洲精品成人网久久久久久 | www.亚洲成在线| 成人毛片免费观看视频| 亚洲国产aⅴ成人精品无吗| 国产传媒在线观看视频免费观看| 黄色免费在线观看网址| 亚洲色无码专区在线观看| 999久久久免费精品播放| 亚洲二区在线视频| 女人18一级毛片免费观看| 亚洲AV无码专区在线厂| 色噜噜亚洲精品中文字幕| 一级毛片免费毛片一级毛片免费| 亚洲精品在线播放| 啦啦啦手机完整免费高清观看| 久久精品国产亚洲av瑜伽| 最新亚洲成av人免费看| 91精品全国免费观看含羞草 | 亚洲成网777777国产精品| 成人免费av一区二区三区| 亚洲精品自在线拍| 国产亚洲福利一区二区免费看| 9久久免费国产精品特黄| 亚洲丰满熟女一区二区v| 亚洲 国产 图片| 免费在线看污视频| 久久亚洲精品国产亚洲老地址| 亚洲国产精品无码久久久久久曰| 免费91最新地址永久入口| 亚洲日本乱码卡2卡3卡新区|