温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么实现Builder模式

发布时间:2021-06-26 17:02:44 来源:亿速云 阅读:139 作者:Leah 栏目:移动开发

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();*/

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI