Android中怎么實(shí)現(xiàn)Builder模式

Android中怎么實(shí)現(xiàn)Builder模式,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)網(wǎng)站建設(shè)提供從項(xiàng)目策劃、軟件開發(fā),軟件安全維護(hù)、網(wǎng)站優(yōu)化(SEO)、網(wǎng)站分析、效果評(píng)估等整套的建站服務(wù),主營(yíng)業(yè)務(wù)為網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè),重慶App定制開發(fā)以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。成都創(chuàng)新互聯(lián)深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!

Builder模式使用鏈?zhǔn)浇Y(jié)構(gòu)創(chuàng)建復(fù)雜對(duì)象,將過(guò)程與結(jié)果分開,創(chuàng)建過(guò)程中可以自行組合。

使用場(chǎng)景

一個(gè)對(duì)象,不同組合,不同順序生成不同的結(jié)果
優(yōu)點(diǎn):封裝性更規(guī)范,程序調(diào)用不用關(guān)系內(nèi)部細(xì)節(jié),注重結(jié)果即可
缺點(diǎn):如果builder對(duì)象過(guò)多,會(huì)加大內(nèi)存消耗

public class TabInfoBean {

 private int count;//Tab的個(gè)數(shù) 必選
 private int currentTab;//默認(rèn)選中的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={"我","是","誰(shuí)"};
  TabInfoBean.TabInfoBuilder tabInfoBuilder=new TabInfoBean.TabInfoBuilder(name,5,0);
  /* TabInfoBean tabInfoBean=tabInfoBuilder
    .setNormalResId()
    .setSelectResId()
    .setNormalTextColor()
    .setSelectTextColor()
    .setNormalTextSizeSp()
    .setSelectTextSizeSp()
    .build();*/

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

當(dāng)前題目:Android中怎么實(shí)現(xiàn)Builder模式
網(wǎng)頁(yè)地址:http://muchs.cn/article28/jpgscp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站、手機(jī)網(wǎng)站建設(shè)面包屑導(dǎo)航、微信小程序、電子商務(wù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管