A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 位雪 中级黑马   /  2012-10-6 15:32  /  2254 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 位丹丹 于 2012-10-6 16:10 编辑
  1. import java.beans.IntrospectionException;
  2. import java.beans.PropertyDescriptor;
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;

  5. public class IntroSspectorTest {

  6. public static void main(String[] args) throws Exception {

  7. ReflectPoint pt1 = new ReflectPoint(3,6);

  8. String propertyName = "x";

  9. Object retVal = getProperty(pt1, propertyName);//此处抽取代码的作用和意义是什么?
  10. System.out.println(retVal);

  11. int value = 9 ;
  12. setProperty(pt1, propertyName, value);//还有此处

  13. System.out.println(pt1.getX());


  14. }

  15. private static void setProperty(Object pt1, String propertyName,
  16. int value) throws IntrospectionException, IllegalAccessException,
  17. InvocationTargetException {
  18. PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,pt1.getClass() );
  19. Method methodSetX = pd2.getWriteMethod();
  20. methodSetX.invoke(pt1, value);
  21. }

  22. private static Object getProperty(Object pt1, String propertyName)
  23. throws IntrospectionException, IllegalAccessException,
  24. InvocationTargetException {
  25. PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,pt1.getClass() );
  26. Method methodGetX = pd1.getReadMethod();
  27. Object retVal = methodGetX.invoke(pt1);
  28. return retVal;
  29. }

  30. }
复制代码
代码抽取之后和不抽取运行结果一样,好像并没简化书写,那么抽取代码的意义是什么呢?望指点

评分

参与人数 1技术分 +1 收起 理由
唐志兵 + 1 赞一个!

查看全部评分

3 个回复

倒序浏览
本帖最后由 黄小贝 于 2012-10-6 16:01 编辑

目测楼主还没有领悟到小函数的魅力~ 你这里代码不够长~~还不够晕~

从Spring找了一个经典的函数来展示一下小函数的魅力~这个函数的作用是什么不需要知道,Spring IOC容器初始化时调用的刷新函数,如果把这些小函数的代码全部丢到这里面,会给读者带来很大的麻烦,维护的时候也很有困难~

把代码分成一个个的小函数,每个函数从名字就可以看出作用是什么,思路很清晰

public void refresh() throws BeansException, IllegalStateException {
                synchronized (this.startupShutdownMonitor) {
                        // Prepare this context for refreshing.
                        prepareRefresh();

                        // Tell the subclass to refresh the internal bean factory.
                        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

                        // Prepare the bean factory for use in this context.
                        prepareBeanFactory(beanFactory);

                        try {
                                // Allows post-processing of the bean factory in context subclasses.
                                postProcessBeanFactory(beanFactory);

                                // Invoke factory processors registered as beans in the context.
                                invokeBeanFactoryPostProcessors(beanFactory);

                                // Register bean processors that intercept bean creation.
                                registerBeanPostProcessors(beanFactory);

                                // Initialize message source for this context.
                                initMessageSource();

                                // Initialize event multicaster for this context.
                                initApplicationEventMulticaster();

                                // Initialize other special beans in specific context subclasses.
                                onRefresh();

                                // Check for listener beans and register them.
                                registerListeners();

                                // Instantiate all remaining (non-lazy-init) singletons.
                                finishBeanFactoryInitialization(beanFactory);

                                // Last step: publish corresponding event.
                                finishRefresh();
                        }

                        catch (BeansException ex) {
                                // Destroy already created singletons to avoid dangling resources.
                                beanFactory.destroySingletons();

                                // Reset 'active' flag.
                                cancelRefresh(ex);

                                // Propagate exception to caller.
                                throw ex;
                        }
                }


抽取方法(extract method)是重构的主要方法之一




评分

参与人数 1技术分 +1 收起 理由
王海宇 + 1

查看全部评分

回复 使用道具 举报
在这里抽取代码主要是便于后期阅读与维护,也可以方便他人阅读。还有就是可以提高代码的重用性。例如多个地方都用到了作用相同的多行代码,就可以将这些代码抽取到方法里面,供多个地方调用
回复 使用道具 举报
"Object retVal = getProperty(pt1, propertyName);// 此处抽取代码的作用和意义是什么?"
  1. PropertyDescriptor pd1 = new PropertyDescriptor(propertyName,
  2.                                 pt1.getClass());
  3.                 Method methodGetX = pd1.getReadMethod();
  4.                 Object retVal = methodGetX.invoke(pt1);
  5.                 return retVal;
复制代码
将以上代码封装在getProperty()方法里不是增加了代码的重用性,并且精简了代码么,这就是抽取代码的作用和意义.



"setProperty(pt1, propertyName, value);// 还有此处"

理由同上.
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马