Android设计模式之Builder模式详解

 更新时间:2017年08月18日 14:49:44   作者:Allure丶  
这篇文章主要为大家详细介绍了Android设计模式之Builder模式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Builder模式使用链式结构创建复杂对象,将过程与结果分开,创建过程中可以自行组合。

使用场景

一个对象,不同组合,不同顺序生成不同的结果
优点:封装性更规范,程序调用不用关系内部细节,注重结果即可
缺点:如果builder对象过多,会加大内存消耗

public class TabInfoBean {

 private int count;//Tab的个数 必选
 private int currentTab;//默认选中的tab 必选
 private String[] tabText;//文字必选


 private int normalResId;//可选
 private int selectResId;//可选
 private int normalTextColor;//可选
 private int selectTextColor;//可选
 private int normalTextSizeSp;//可选
 private int selectTextSizeSp;//可选


 private TabInfoBean(TabInfoBuilder builder) {
  this.tabText = builder.tabText;
  this.count = builder.count;
  this.currentTab = builder.currentTab;

  this.normalResId = builder.normalResId;
  this.selectResId = builder.selectResId;

  this.normalTextColor = builder.normalTextColor;
  this.selectTextColor = builder.selectTextColor;
  this.normalTextSizeSp = builder.normalTextSizeSp;
  this.selectTextSizeSp = builder.selectTextSizeSp;
 }

 public int getCount() {
  return count;
 }

 public void setCount(int count) {
  this.count = count;
 }

 public int getCurrentTab() {
  return currentTab;
 }

 public void setCurrentTab(int currentTab) {
  this.currentTab = currentTab;
 }

 public int getNormalResId() {
  return normalResId;
 }

 public void setNormalResId(int normalResId) {
  this.normalResId = normalResId;
 }

 public int getSelectResId() {
  return selectResId;
 }

 public void setSelectResId(int selectResId) {
  this.selectResId = selectResId;
 }

 public int getNormalTextColor() {
  return normalTextColor;
 }

 public void setNormalTextColor(int normalTextColor) {
  this.normalTextColor = normalTextColor;
 }

 public int getSelectTextColor() {
  return selectTextColor;
 }

 public void setSelectTextColor(int selectTextColor) {
  this.selectTextColor = selectTextColor;
 }

 public String[] getTabText() {
  return tabText;
 }

 public void setTabText(String[] tabText) {
  this.tabText = tabText;
 }


 public int getNormalTextSizeSp() {
  return normalTextSizeSp;
 }

 public void setNormalTextSizeSp(int normalTextSizeSp) {
  this.normalTextSizeSp = normalTextSizeSp;
 }

 public int getSelectTextSizeSp() {
  return selectTextSizeSp;
 }

 public void setSelectTextSizeSp(int selectTextSizeSp) {
  this.selectTextSizeSp = selectTextSizeSp;
 }

 public static class TabInfoBuilder {
  private int count;
  private int currentTab;
  private String[] tabText;

  private int normalResId;
  private int selectResId;
  private int normalTextColor;
  private int selectTextColor;
  private int normalTextSizeSp;//可选
  private int selectTextSizeSp;//可选

  public TabInfoBuilder(String[] tabText, int count, int currentTab) {
   this.tabText = tabText;
   this.count = count;
   this.currentTab = currentTab;
  }

  public TabInfoBuilder setNormalResId(int normalResId) {
   this.normalResId = normalResId;
   return this;
  }

  public TabInfoBuilder setSelectResId(int selectResId) {
   this.selectResId = selectResId;
   return this;
  }

  public TabInfoBuilder setNormalTextColor(int normalTextColor) {
   this.normalTextColor = normalTextColor;
   return this;
  }

  public TabInfoBuilder setSelectTextColor(int selectTextColor) {
   this.selectTextColor = selectTextColor;
   return this;
  }

  public TabInfoBuilder setNormalTextSizeSp(int size) {
   this.normalTextSizeSp = size;
   return this;
  }

  public TabInfoBuilder setSelectTextSizeSp(int size) {
   this.selectTextSizeSp = size;
   return this;
  }


  public TabInfoBean build() {
   return new TabInfoBean(this);
  }
 }
}

调用方式

String[] name={"我","是","谁"};
  TabInfoBean.TabInfoBuilder tabInfoBuilder=new TabInfoBean.TabInfoBuilder(name,5,0);
  /* TabInfoBean tabInfoBean=tabInfoBuilder
    .setNormalResId()
    .setSelectResId()
    .setNormalTextColor()
    .setSelectTextColor()
    .setNormalTextSizeSp()
    .setSelectTextSizeSp()
    .build();*/

github代码地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android仿微信菜单(Menu)(使用C#和Java分别实现)

    Android仿微信菜单(Menu)(使用C#和Java分别实现)

    这篇文章主要介绍了Android仿微信菜单(Menu)(使用C#和Java分别实现),本文分别给出C#和Java版的运行效果及实现代码,需要的朋友可以参考下
    2015-06-06
  • Android中ConstraintLayout约束布局的最全详细解析

    Android中ConstraintLayout约束布局的最全详细解析

    ConstraintLayout是Google在Google I/O 2016大会上发布的一种新的布局容器(ViewGroup),它支持以灵活的方式来放置子控件和调整子控件的大小,下面这篇文章主要给大家介绍了关于Android中ConstraintLayout约束布局详细解析的相关资料,需要的朋友可以参考下
    2022-08-08
  • Android ViewFlipper简单应用

    Android ViewFlipper简单应用

    这篇文章主要为大家详细介绍了Android ViewFlipper简单应用,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • Android编程实现监控apk安装,卸载,替换的方法

    Android编程实现监控apk安装,卸载,替换的方法

    这篇文章主要介绍了Android编程实现监控apk安装,卸载,替换的方法,涉及Android基于Intent监控apk状态的功能实现技巧,需要的朋友可以参考下
    2016-01-01
  • Android中自定义Window Title样式实例

    Android中自定义Window Title样式实例

    这篇文章主要介绍了Android中自定义Window Title样式实例,本文给出效果预览和实现方法,需要的朋友可以参考下
    2015-01-01
  • Android Spinner 下拉菜单的使用

    Android Spinner 下拉菜单的使用

    Android 中下拉菜单,即如html中的<select>,关键在于调用setDropDownViewResource方法,以XML的方式定义下拉菜单要显示的模样
    2013-04-04
  • android 之listview 优化方法

    android 之listview 优化方法

    这篇文章主要介绍了android 之listview 优化方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • 详解Glide最新版V4使用指南

    详解Glide最新版V4使用指南

    这篇文章主要介绍了详解Glide最新版V4使用指南,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • android cts测试方法及步骤详解

    android cts测试方法及步骤详解

    这篇文章给大家分享了android cts测试方法及步骤的相关知识点,有兴趣的朋友跟着学习习吧。
    2018-07-07
  • Android自定义view圆并随手指移动

    Android自定义view圆并随手指移动

    这篇文章主要为大家详细介绍了Android自定义view圆并随手指移动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08

最新评论