Androud 如何有效减少重复代码

前言


       重复的代码一直都是可维护性的大敌,重构的重要任务之一也就是要去除掉重复的代码,有效的减少重复代码,可以大大提高软件的扩展性。

       在Android开发中,很容易产生重复的代码。因为Android是组件,模板式开发,每个页面都是独立用Activity或Fragment实现,布局文件都是用XML方式去写,所以很容易造成代码的重复,虽然二个页长的差不多,但毕竟是二个Activity,于是就拷一份出来,改吧改吧就成了。

       那么我们如何做才能去掉重复的代码呢?

一、使用include标签引用重复布局

二、使用style定义样式

三、使用ViewStub减少整体布局的重复

四、多使用应用资源

五、代码的抽象与继承

六、总结


一、使用include标签引用重复布局


       标签是减少布局重复的利器,它的作用是把另外一个布局文件全部无修改式的嵌入到标签所在的位置。这与C/C++语言的预处理指令#include是一样的。在WEB框架式开发中,也非常常用HTML模板,其中也有类似的include。目的也是减少代码的重复。

       要想把include用的恰到好处,首先要做的就是把整体布局模块化,从整体的布局出发,找出可复用的局部布局或布局组合,把它们放入单独一个布局文件中,然后在其他的地方就可以include了。

例如:

test.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"   
    android:orientation="vertical">  
  
      
    <Button   
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:textSize="16dp"  
        android:gravity="center"  
        android:layout_marginTop="5dp"  
        android:layout_marginBottom="5dp"  
        android:text="1"  />  
      
    <View   
        android:layout_width="match_parent"  
        android:layout_height="0.5dp"  
        android:background="#BFBFBF"/>  
      
      
    <Button   
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:textSize="16dp"  
        android:gravity="center"  
        android:layout_marginTop="5dp"  
        android:layout_marginBottom="5dp"  
        android:text="2"  />  
      
    <View   
        android:layout_width="match_parent"  
        android:layout_height="0.5dp"  
        android:background="#BFBFBF"/>  
          
    <Button   
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:textSize="16dp"  
        android:gravity="center"  
        android:layout_marginTop="5dp"  
        android:layout_marginBottom="5dp"  
        android:text="3"   />  
                     
</LinearLayout>

       这段布局是我们正常情况下的写法,从布局中我们可以看出View标签代码是一样的,那么我们就可以把View单独写在一个xml文件里,然后用include标签引用它即可。

简化后的布局代码如下:

test_1.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:text="1"  />
    
    <include layout="@layout/divider_view"/>
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:text="2"  />
    
    <include layout="@layout/divider_view"/>
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:gravity="center"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:text="3"  />
</LinearLayout>

 
 divider_view.xml 
 

<?xml version="1.0" encoding="utf-8"?>
<View xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:background="#BFBFBF">

</View> 

       怎么样,是不是比原来的布局代码简洁直观多了,而且实现出来的效果也一样。当然这是单个View重复,那么多个View组重复也是一样的。

       使用include标签还有一个好处,就比如你要修改View的样式,在未使用include之前,你需要把test.xml中的所有View都要修改一遍,但是使用include之后,你只需要修改divider_view.xml中的View即可。


二、使用style定义样式


       在我们开发应用的过程中,会有很多控件的样式是一样的,我们一般会用复制-粘贴来使用,这样就会有大量重复代码出现,而且如果我们某一天要修改该控件的样式了,那我们需要找到所有这些控件,一个一个的修改样式,但是我们如果使用style来定义样式的话,不仅会减少大量重复代码,而且修改样式也变的非常灵活。

       就比如test_1.xml布局文件中有三个Button控件,只有text内容不同,那么我们如何把Button的共同点提取到style中呢?很简单,在styles.xml下 新增一对<style>标签,为该style命名,如命名为DemoBtn,然后在<style>标签内 使用<item>标签来存放控件的属性,例如 android:layout_width="match_parent"     ,用<item>来表示就是 <item name="android:layout_width">match_parent</item>,name是控件的属性名,标签之间则是属性值

按照这样的方法,把所有共同属性都添加到DemoBtn这一style中即可。

<style name="DemoBtn">  
      
    <item name="android:layout_width">match_parent</item>  
    <item name="android:layout_height">wrap_content</item>  
    <item name="android:textSize">16dp</item>  
    <item name="android:gravity">center</item>  
    <item name="android:layout_marginTop">5dp</item>  
    <item name="android:layout_marginBottom">5dp</item>  
</style>
       最后回到布局中,把三个Button的共同属性全都删除掉,换为 style ="@style/DemoBtn"   ,就能达到原来的效果,1行代码代替6行代码,代码现在更加美观了。

test_2.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"   
    android:orientation="vertical">  
    
    <Button   
        style="@style/DemoBtn"  
        android:text="1"    />  
      
    <include layout="@layout/divider_view"/>  
         
    <Button   
        style="@style/DemoBtn"  
        android:text="2"   />  
      
    <include layout="@layout/divider_view"/>  
         
    <Button   
    style="@style/DemoBtn"  
        android:text="3"   />  
               
</LinearLayout>

       经过include标签引用和style样式提取,在看我们的布局文件,是不是感觉很清爽了。而且需要修改样式的话,只需要在style中修改就可以了。


三、使用ViewStub减少整体布局的重复


       前面是找出布局中的可复用布局组合,include以减少重复。但有些时候是反过来的,也就是说有几个页面,它们整体的一样的,但是某个局部是不同的,这个时候include就不行了。这个时候就可以使用一样的整体布局+ViewStub来做布局。

       ViewStub是一个轻量级别的,不可见的View,当ViewStub被设为visible时,或者显示调用layout()时,才会去把它所指向的布局渲染出来,所以它非常适合处理整体相同,局部不同的情况。关于ViewStub的使用可以参考Android实战技巧:ViewStub的应用。 具体的策略是:

  • 规划整体布局,抽象出共同的布局,把可变的布局识别出来
  • 写整体布局,对于可变的局部布局用ViewStub替代
  • 用一个基Fragment来操作整体布局。
  • 创建基Fragment的子类,每个子类,用真正的布局来替换ViewStub。


四、多使用应用资源


       这点是非常重要的,Android的强大之处在于,所有的资源的指定都可以用引用,而非直接写死,直接写死就会出现重复代码,比如颜色,背影,字串,布局,ID,度量(dimen),风格等等。那么,我们在使用的时候,也尽可能的使用引用,这样非常易于复用,修改和定制,从而也就更方便复用。


五、代码的抽象与继承


       从代码上去除重复的代码就是用通用的重构技巧,比如提炼方法,抽象基类,提炼常量等。


六、总结


       其实代码的去重复的关键都在于要分析出可变与不可变,共性和特性,这是抽象与封装的基础。这个没有直接可操作性的建议,只能靠自己平时多多积累,以及遇到问题时多多思考。

       另外,就是对于重复的定义是达到三次及三次以上。如果仅出现二次,并且,无可能出现别一次,这个时候其时,要不要去重复有在商榷,写第二次时,花时间重构,与拷贝或重新实现,其实代码差不多。当发现第三次实现某个东西时,就要考虑好好的重构一下,减少重复。

       还有,就是,在项目开始不要考虑的太多,不要过度设计。孤认为,不过度设计,更不要过早优化,就根据需求和进度和发展状况综合来看。当出现了重复,需要重构时就立马去做,这样就不会出问题。但如果没能及时去做重构,欠了债,那么由于涉及代码,功能和模块都比较多时,再去重构,难度大,风险也大。就好比房屋的维护保养,当出现灰尘时就去打扫,很容易,个把小时就搞定了,但如果一直拖着,一年才打扫一次,可能要十天半个月才能完事。


猜你喜欢

转载自blog.csdn.net/wangzhongshun/article/details/78738217