国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android設計模式之Builder模式詳解

2019-10-22 18:29:51
字體:
來源:轉載
供稿:網友

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江华| 衡山县| 嘉兴市| 三门县| 班玛县| 怀安县| 盐城市| 上饶县| 沙河市| 招远市| 繁昌县| 龙门县| 安西县| 江阴市| 湛江市| 阿勒泰市| 连江县| 唐河县| 桓台县| 紫金县| 兴安县| 怀来县| 商水县| 大名县| 晋江市| 望都县| 岳阳市| 赤峰市| 三台县| 马边| 洪洞县| 郸城县| 调兵山市| 泊头市| 乐亭县| 昌邑市| 波密县| 遂平县| 青神县| 手游| 繁昌县|