<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)限信息,并且封裝為樹(shù)形結(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 閱讀(3087) 評(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视频在线精品免费观看6| 在线成人爽a毛片免费软件| 国产午夜鲁丝片AV无码免费| 亚洲六月丁香六月婷婷蜜芽| 在线观看免费av网站| 久久精品a亚洲国产v高清不卡 | 妞干网在线免费观看| 亚洲人成免费电影| 国产人在线成免费视频| 亚洲永久在线观看| 女人18毛片免费观看| 亚洲AV成人片无码网站| 免费精品国产自产拍观看| 日本免费精品一区二区三区| 亚洲午夜激情视频| 国偷自产一区二区免费视频| 亚洲午夜视频在线观看| 无码国产精品一区二区免费式直播| 亚洲日韩国产精品乱-久| 国产成人精品免费视频软件| 思思久久99热免费精品6| 亚洲乱码中文字幕综合 | 国产精品免费精品自在线观看| 亚洲中文久久精品无码1| 天天天欲色欲色WWW免费| 无码人妻一区二区三区免费视频 | 免费在线观看污网站| 二个人看的www免费视频| 亚洲欧洲日韩国产综合在线二区| 精品一区二区三区免费毛片爱 | 亚洲精品第一国产综合精品| 国产精品视频永久免费播放| 午夜亚洲乱码伦小说区69堂| 国产亚洲色婷婷久久99精品 | 日本免费一二区在线电影| 一区二区三区免费在线视频| 亚洲精品综合一二三区在线| 好男人看视频免费2019中文| 中文字幕在线视频免费| 国产精品高清视亚洲精品| 亚洲一区二区精品视频|