Android设计模式之(2)----Builder模式_android builder builder = new builder()-程序员宅基地

技术标签: android  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代码地址

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/asddavid/article/details/77233131

智能推荐

Hadoop集群搭建(非高可用)_高可用集群hadoop-程序员宅基地

文章浏览阅读396次。非高可用集群搭建 第一部分环境准备 =============================================================================== 1. 安装好linux 2. *安装VMTools 3. 关闭防火墙 sudo service iptables stop sudo chkconfig ipt..._高可用集群hadoop

C语言system函数使用-程序员宅基地

文章浏览阅读10w+次,点赞270次,收藏1.4k次。函数原型包含在头文件 “stdlib.h” 中int system(const char * command)函数功能执行 dos(windows系统) 或 shell(Linux/Unix系统) 命令,参数字符串command为命令名。另,在windows系统下参数字符串不区分大小写。说明:在windows系统中,system函数直接在控制台调用一个command命令。在L..._system函数

第3课 计算机病毒的防治反思,《计算机病毒及其防治》说课稿 三篇.doc-程序员宅基地

文章浏览阅读230次。文档介绍:《计算机病毒及其防治》说课稿各位评委教师: 大家好! 我是来自荆门市石化中学的信息技术教师赵城,今天我说课是内容是《计算机病毒及其防治》,这是一节理论实践课, 下面具体阐述一下我设计这节课的具体设计思路,并希望能得到各位专家和老师的指导。第一部分:说教材一、教材分析《计算机病毒及其防治》是华中师范大学出版社全日制普通高级中学教科书《信息技术》第四册“信息安全”中的第四部分和第五部分内容,...

vue 移动端word、excel、pdf预览本地测试案例_vue移动端项目预览文件的插件-程序员宅基地

文章浏览阅读4.1k次。第一步新建一个vue项目。打开控制命令行程序(CMD),运行命令: vue init webpack “项目名称”。第二步安装依赖,在控制命令行程序(CMD),运行命令:npm install 或者 cnpm install.实现预览这里先以word为例。首先运行命令:npm install mammoth,这是预览word的一个插件,必须要安装;在static文件夹下放一个.docx为后缀的word文件:然后在页面引入即可:<template> <div cla_vue移动端项目预览文件的插件

apple configurator 2 获取appstore ipa包_appleconfigurator2怎么下载-程序员宅基地

文章浏览阅读2.6k次。apple configurator 2 获取appstore ipa包_appleconfigurator2怎么下载

大学生职业生涯规划_学代码的职业生涯规划-程序员宅基地

文章浏览阅读5.9k次,点赞8次,收藏18次。一、自我分析  1.个人基本情况  性格特点:性格外向,待人诚恳,交际能力良好做事认真,能吃苦耐劳,有良好的学习能力  2.职业兴趣  做一名机械工程师是我的第一职业兴趣!做一名机械工程师需要具备基本的机械加工工艺知识,比如机床选择、刀具选择、切削参数、走刀路径;基本的动手能力,而我正是从小就开始锻炼自己的动手能力,拆装、修理方法的选择对我来说基本上从小学就开始学习_学代码的职业生涯规划

随便推点

AES加密算法_aes256加密算法-程序员宅基地

文章浏览阅读2.1k次。密钥类型AES-128:128位比特(16字节) AES-192:192位比特(24字节) AES-256:256位比特(32字节)一般简短数据采用AES-128,也就是16字节,少部分采用AES-256。填充方式NoPadding:不填充,只能加密长度为16倍数的数据,一般不适用; Zeros:补0,如果原数据长度恰好是16的倍数,也要补16个0; ISO10126:最后一个字节是填充的字节数(包括最后一字节),其他全部填随机数1 2 3 4 5 6 7 8 9 10 –.._aes256加密算法

python oracle转mysql_python 取oracle数据转存至mysql-程序员宅基地

文章浏览阅读137次。由于grafana的oracle插件需要付费,所以只能想想办法,于是就用Oracle的数据转到mysql数据库里面。其实也很简单,需要提前安装好python和oracle数据库驱动cx_oracle 和MySQL 驱动,具体可以自己搜索。脚本如下#!/usr/bin/python# -*- coding: UTF-8 -*-import cx_Oracle #导入包import MySQLdbim..._str(len(list))

一定要学会的vsCode格式化整理代码的快捷键,再也不用手动调格式了_cscode格式化代码快捷键-程序员宅基地

文章浏览阅读2.7w次,点赞16次,收藏45次。开发过程中经常遇到代码杂乱的情况,每次手动排列都需要浪费大量时间,如果能配置自动整理代码既能让代码赏心悦目也就不用再浪费排列的时间1、在vscode编辑器中插件应用里找到vetur并安装点击左侧最后一个图片查看已有的插件,我这已经加了Vetur的插件,如果没有的同学直接在上面搜索自动添加就可以2、设置打开file(文件)----->preferences(首选项)------>settings(设置),搜索vetur.format.defaultFormatter.html会_cscode格式化代码快捷键

angular入门基本操作(node.js安装、angular安装、镜像源安装、visual studio code安装、git常用命令)_angular镜像安装-程序员宅基地

文章浏览阅读655次。angular入门基本操作一、下载并安装Node.js​ 安装步骤 下载链接​ 为什么安装Node.js​ Angular、Angular CLI 和 Angular 应用都依赖于 npm包(node.js package manage)中提供的特性和功能。要想 下载并安装 npm包,就必须拥有一个 npm 包管理器,而Node.js 已经默认安装了它。二、安装Angular CLI..._angular镜像安装

Android开发实用必备的几款插件,分析Android未来几年的发展前景,Android岗_android 2021 好用的插件-程序员宅基地

文章浏览阅读170次。前言近日,字节跳动正式启动了2021届秋季校园招聘,为应届毕业生开放超过6000个工作岗位。这一数字超过了该公司往年秋招规模,并与其今年春招规模持平。全年校招人数共计超过1万2千人,远高于同类型互联网公司,体现了字节跳动保持业务快速增长,重视对优秀人才的持续投入。字节跳动校园招聘负责人介绍,该项招聘主要面向2021届毕业生,即2020 年9月至2021年8月期间毕业的大学生群体。这批岗位覆盖字节跳动10多项产品和业务,既包括今日头条、抖音、西瓜视频等旗舰产品,也包括懂车帝、幸福里、番茄小说等垂类应用,以_android 2021 好用的插件

jsoncpp使用笔记_jsoncpp asstring-程序员宅基地

文章浏览阅读528次。jsoncpp 中部分函数一览:Value类型判断值获取操作isNll()(bool类型)asBool()数组操作[index]isBool()asInt()isMember(key):boolisInt()asString()数组添加append(value)isString()asDouble()removeMenber(key):ValueisArray()asArray()removeIndex(index,value):._jsoncpp asstring

推荐文章

热门文章

相关标签