Android设计模式之Builder模式详解

发布时间 - 2026-01-11 02:51:54    点击率:

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  # 设计模式  # Builder  # Android对话框AlertDialog.Builder使用方法详解  # Android使alertDialog.builder不会点击外面和按返回键消失的方法  # Android中用Builder模式自定义Dialog的方法  # Android编程设计模式之Builder模式实例详解  # Android Notification.Builder通知案例分享  # Android设计模式之Builder模式解析  # Android Binder入门学习笔记  # 可选  # 链式  # 大家多多  # 过程中  # 可以自行  # tab  # String  # currentTab  # count 


相关栏目: 【 网站优化151355 】 【 网络推广146373 】 【 网络技术251813 】 【 AI营销90571


相关推荐: Python数据仓库与ETL构建实战_Airflow调度流程详解  Python并发异常传播_错误处理解析【教程】  如何在橙子建站上传落地页?操作指南详解  Laravel如何部署到服务器_线上部署Laravel项目的完整流程与步骤  如何在云主机上快速搭建网站?  Python文件异常处理策略_健壮性说明【指导】  如何在腾讯云免费申请建站?  php读取心率传感器数据怎么弄_php获取max30100的心率值【指南】  如何在Ubuntu系统下快速搭建WordPress个人网站?  微信小程序制作网站有哪些,微信小程序需要做网站吗?  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  Swift开发中switch语句值绑定模式  成都品牌网站制作公司,成都营业执照年报网上怎么办理?  详解Android中Activity的四大启动模式实验简述  Laravel如何使用Blade组件和插槽?(Component代码示例)  敲碗10年!Mac系列传将迎来「触控与联网」双革新  Laravel路由怎么定义_Laravel核心路由系统完全入门指南  在Oracle关闭情况下如何修改spfile的参数  创业网站制作流程,创业网站可靠吗?  佐糖AI抠图怎样调整抠图精度_佐糖AI精度调整与放大细化操作【攻略】  html如何与html链接_实现多个HTML页面互相链接【互相】  装修招标网站设计制作流程,装修招标流程?  php后缀怎么变mp4格式错误_修改扩展名提示格式不对怎么办【技巧】  Laravel如何构建RESTful API_Laravel标准化API接口开发指南  成都网站制作公司哪家好,四川省职工服务网是做什么用?  Laravel 419 page expired怎么解决_Laravel CSRF令牌过期处理  Laravel如何创建和注册中间件_Laravel中间件编写与应用流程  Laravel如何获取当前登录用户信息_Laravel Auth门面使用与Session用户读取【技巧】  如何挑选优质建站一级代理提升网站排名?  利用python获取某年中每个月的第一天和最后一天  湖南网站制作公司,湖南上善若水科技有限公司做什么的?  ChatGPT怎么生成Excel公式_ChatGPT公式生成方法【指南】  做企业网站制作流程,企业网站制作基本流程有哪些?  网站制作价目表怎么做,珍爱网婚介费用多少?  Laravel如何使用Spatie Media Library_Laravel图片上传管理与缩略图生成【步骤】  济南网站建设制作公司,室内设计网站一般都有哪些功能?  JavaScript中的标签模板是什么_它如何扩展字符串功能  Laravel如何配置和使用缓存?(Redis代码示例)  Laravel请求验证怎么写_Laravel Validator自定义表单验证规则教程  中山网站制作网页,中山新生登记系统登记流程?  Laravel怎么做缓存_Laravel Cache系统提升应用速度的策略与技巧  ,南京靠谱的征婚网站?  Laravel如何实现数据导出到PDF_Laravel使用snappy生成网页快照PDF【方案】  iOS UIView常见属性方法小结  详解CentOS6.5 安装 MySQL5.1.71的方法  JavaScript如何实现继承_有哪些常用方法  Laravel如何集成Inertia.js与Vue/React?(安装配置)  Laravel如何实现API资源集合?(Resource Collection教程)  Laravel如何保护应用免受CSRF攻击?(原理和示例)  香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南