Android設(shè)計模式之Builder模式詳解
Builder模式使用鏈式結(jié)構(gòu)創(chuàng)建復(fù)雜對象,將過程與結(jié)果分開,創(chuàng)建過程中可以自行組合。
使用場景
一個對象,不同組合,不同順序生成不同的結(jié)果
優(yōu)點:封裝性更規(guī)范,程序調(diào)用不用關(guān)系內(nèi)部細節(jié),注重結(jié)果即可
缺點:如果builder對象過多,會加大內(nèi)存消耗
public class TabInfoBean { private int count;//Tab的個數(shù) 必選 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); } } }
調(diào)用方式
String[] name={"我","是","誰"}; TabInfoBean.TabInfoBuilder tabInfoBuilder=new TabInfoBean.TabInfoBuilder(name,5,0); /* TabInfoBean tabInfoBean=tabInfoBuilder .setNormalResId() .setSelectResId() .setNormalTextColor() .setSelectTextColor() .setNormalTextSizeSp() .setSelectTextSizeSp() .build();*/
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)之使用ExifInterface獲取拍照后的圖片屬性
這篇文章主要介紹了Android開發(fā)之使用ExifInterface獲取拍照后的圖片屬性,較為詳細的分析了ExifInterface類操作圖片的具體使用技巧,需要的朋友可以參考下2016-01-01擁抱kotlin之如何習慣使用kotlin高階函數(shù)
這篇文章主要給大家介紹了關(guān)于擁抱kotlin之如何習慣使用kotlin高階函數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用kotlin具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-12-12android調(diào)用webservice接口獲取信息
這篇文章主要為大家詳細介紹了android調(diào)用webservice接口獲取信息,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11AFURLSessionManager 上傳下載使用代碼說明
本文通過代碼給大家介紹了AFURLSessionManager 上傳下載使用說明,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-09-09Android實現(xiàn)可拖拽的GridView效果長按可拖拽刪除數(shù)據(jù)源
這篇文章主要介紹了Android實現(xiàn)可拖拽的GridView效果長按可拖拽刪除數(shù)據(jù)源,要實現(xiàn)的基本功能是長按,移到垃圾桶,刪除數(shù)據(jù),需要的朋友可以參考下2017-12-12Android開發(fā)中Toast顯示消息的方法小結(jié)
這篇文章主要介紹了Android開發(fā)中Toast顯示消息的方法,結(jié)合實例形式總結(jié)分析了Toast的功能、創(chuàng)建Toast對象及調(diào)用相關(guān)函數(shù)顯示消息提示框的操作技巧,需要的朋友可以參考下2016-10-10在Android中如何使用DataBinding詳解(Kotlin)
這篇文章主要給大家介紹了關(guān)于在Android中如何使用DataBinding(Kotlin)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11