Android设计模式之Builder模式详解

作者:Allure丶 时间:2022-01-17 12:29:02 

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代码地址

来源:http://blog.csdn.net/asddavid/article/details/77233131

标签:Android,设计模式,Builder
0
投稿

猜你喜欢

  • Java实现二分查找的变种

    2023-11-07 11:26:28
  • 解决IDEA克隆代码后在右下角没有git分支的问题

    2021-11-23 09:43:19
  • Java多线程 两阶段终止模式Two-Phase Termination Patter

    2023-11-29 04:47:04
  • Android实现自动匹配关键字并且标红功能

    2023-05-29 06:19:28
  • C# 7.0中解构功能详解

    2022-08-11 21:06:34
  • SpringBoot的API文档生成工具SpringDoc使用详解

    2021-11-09 07:15:45
  • 解决RestTemplate 请求url中包含百分号 会被转义成25的问题

    2022-11-01 22:59:51
  • C#线性渐变画刷LinearGradientBrush用法实例

    2022-01-21 05:29:43
  • 解析Java线程同步锁的选择方法

    2023-02-08 11:02:32
  • swagger如何返回map字段注释

    2023-02-22 08:56:27
  • SpringBoot自动装配原理详解

    2023-07-26 08:44:46
  • Kotlin协程launch启动流程原理详解

    2021-10-31 15:47:22
  • SpringBoot2.0+阿里巴巴Sentinel动态限流实战(附源码)

    2021-05-25 18:52:47
  • java使用归并删除法删除二叉树中节点的方法

    2022-03-31 23:06:12
  • SpringBoot实现拦截器、过滤器、监听器过程解析

    2023-07-01 02:34:52
  • 在WinForm中发送HTTP请求的实现方法

    2023-01-28 10:47:35
  • C#实现钟表程序设计

    2023-01-01 06:38:42
  • 浅谈十个常见的Java异常出现原因

    2022-10-04 07:58:50
  • Mybatis动态调用表名和字段名的解决方法

    2022-03-18 16:54:14
  • C# 格式化字符首字母大写的方法

    2022-04-28 00:05:12
  • asp之家 软件编程 m.aspxhome.com