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 阅读(3052) 评论(1)  编辑  收藏

评论

# re: 重构—利用反射合并函数  回复  更多评论   

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

只有注册用户登录后才能发表评论。


网站导航: