Android设计模式之Builder模式详解
作者:Allure丶 发布时间:2022-01-17 12:29:02
标签: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代码地址
来源:http://blog.csdn.net/asddavid/article/details/77233131


猜你喜欢
- 本文实例为大家分享了unity使用socket实现聊天室功能的具体代码,供大家参考,具体内容如下示例:什么是Socket:Socket(套接
- 前言其实很多人都会碰到文本不对齐,文字不对齐的情况,但是只要不明显被提出,一般都会置之不理。我关注这个问题是因为有个老哥问我倒计时的时候,1
- 问题描述:使用Design包的TabLayout实现类似网易选项卡动态滑动效果的时候,使用addTab()方法给TabLayout动态添加标
- Java 的线程支持提供了一些便捷的工具方法,通过这些便捷的工具方法可以很好地控制线程的执行。join 线程Thread 提供了让一个线程等
- 在使用IDEA写代码的时候,打开tabs都挤在一行,当打开页面过多的时候,前面的页面无法直观看到,非常不方便。通过简单设置就可以实现tabs
- 前言Queue 也是 Java 集合框架中定义的一种接口,直接继承自 Collection 接口。除了基本的 Collection 接口规定
- string 类型是C#的基元类型之一,它是一个引用类型,对应FCL中的System.String类型。string 类型和普通的引用类型相
- Java序列化是将一个对象编码成一个字节流,反序列化将字节流编码转换成一个对象。 序列化是
- 原文是 java ,现在将它翻译成 C# ,并对代码重新编排整理,博主是一个今年刚出来的应届毕业生,不足之处请多多包涵。根据银行卡号判断所属
- 开发环境安装JDK和JRE下载安装文件并安装:jdk-8u11-windows-i586.exejre-8u11-windows-i586.
- 读取resources下文件的方法网上有问答如下:问:new FileInputStream("src/main/resource
- 在使用NavigationPage导航的时候, 我们可以给里面添加一些功能按钮, 如下所示:<ContentPage.ToolbarI
- 如下所示:class Program {
- 面试题:1.如何保证多线程下 i++ 结果正确?2.一个线程如果出现了运行时异常会怎么样?3.一个线程运行时发生异常会怎样?为了避免临界区的
- 前言在 Java 中通常对一些方法进行一些注解操作,但是很多注解在 Java 代码上没有问题,如果切换到 Kotlin 上时,如果继续使用这
- 实例如下:static bool CheckPowerOfTwo(ulong num){ return num > 0 &
- 目录闲言碎语:背景Actuator介绍Rest方法来查看Actuatorpom.xml引入Actuator依赖配置application.y
- 目录前言:一、餐馆合并菜单二、改进菜单实现三、迭代器模式总结前言:迭代器模式平时用的不多,因为不管C#还是Java都已经帮我封装了,但是你是
- 当数据量比较大的时候,我们就需要考虑读写分离了,也就是动态切换数据库连接,对指定的数据库进行操作。在spring中实现动态的切换无非就是利用
- 实现方式通过挨个罗列的方式一次复制子对象是非常耗费人力的,如果子对象是引用类型,则还要需要考虑是否对子对象进一步深拷贝。实际应用中,一个类如