建造者模式builder,建造builder

來源:互聯網
上載者:User

建造者模式builder,建造builder

緒論:      
那麼對於Android初級來說,Builder設計模式可能在我們開發中用過的很少,但是我們可能見過,我們經常用的AlterDialog.Builder就是一種建造者模式。那麼到底什麼是建造者模式呢?下面我們來看看它的標準定義:

定義:
將一個複雜物件的構建與它的表示分離,使得同樣的構建過程可以建立不同的表示,同時可以分步的構造每一部分。
看起來比較抽象,也不知道具體是什麼意思?下面我舉一個例子,通過代碼來看看什麼是建造者模式

舉例:

public class ImageBean {      private int id; //圖片id      private String path;    //圖片路徑      private long size;  //圖片大小      private String type;    //圖片類型        public ImageBean() {      }        public ImageBean(int id, String path, long size, String type) {          this.id = id;          this.path = path;          this.size = size;          this.type = type;      }        public int getId() {          return id;      }        public void setId(int id) {          this.id = id;      }        public String getPath() {          return path;      }        public void setPath(String path) {          this.path = path;      }        public long getSize() {          return size;      }        public void setSize(long size) {          this.size = size;      }        public String getType() {          return type;      }        public void setType(String type) {          this.type = type;      }  }  

我們定義了一個ImageBean實體,裡麵包含id、path、size、type屬性,其中還包含空的構造方法和一個包含參數的構造。這樣我們在聲明這個實體的時候可以給這個實體賦值。

ImageBean ib = new ImageBean(1,"xxxx","54321","jpg");實體的參數個數少點還好,我們能記住構造的參數,如果這個實體有N個參數呢?我們怎樣知道參數的順序呢?代
碼的可讀性並不高,如果不是你自己寫的,別人根本看不懂,即便是自己寫的,時間長了的話恐怕我們也都忘了。那麼Builder模式就可以解決這種情況。

我們可以給ImageBean建立一個Builder:
public class ImageBean {      private int id; //圖片id      private String path;    //圖片路徑      private long size;  //圖片大小      private String type;    //圖片類型        public ImageBean() {      }        public ImageBean(Builder builder) {          this.id = builder.id;          this.path = builder.path;          this.size = builder.size;          this.type = builder.type;      }        public int getId() {          return id;      }        public void setId(int id) {          this.id = id;      }        public String getPath() {          return path;      }        public void setPath(String path) {          this.path = path;      }        public long getSize() {          return size;      }        public void setSize(long size) {          this.size = size;      }        public String getType() {          return type;      }        public void setType(String type) {          this.type = type;      }            public static class Builder{          private int id;          private String path;          private long size;          private String type;                    public Builder id(int id){              this.id = id;              return this;          }                    public Builder path(String path){              this.path = path;              return this;          }          public Builder size(long size){              this.size = size;              return this;          }          public Builder type(String type){              this.type = type;              return this;          }                    public ImageBean build(){              return new ImageBean(this);          }      }  }  

  

ImageBean.Builder builder = new ImageBean.Builder();  ImageBean ib = builder.id(11).path("xxx").size(1111).type("jpg").build();  

  

可以看到ImageBean的構造參數改成了這個builder,然後定義了一個和ImageBean一樣的Builder,傳回值都是builder對象。這就是定義裡面說的構造和表示分離,構造是ImageBean中來構造,而顯示則在Builder中顯示。也就是分布構造每一部分。
以上只是builder設計模式的一種用法

總結:
在以下情況使用Build模式:
1 當建立複雜物件的演算法應該獨立於該對象的組成部分以及它們的裝配方式時。
2 當構造過程必須允許被構造的對象有不同的表示時。
3 Builder模式要解決的也正是這樣的問題:
  當我們要建立的對象很複雜的時候(通常是由很多其他的對象組合而成),
  我們要複雜物件的建立過程和這個對象的表示(展示)分離開來,
  這樣做的好處就是通過一步步的進行複雜物件的構建,
  由於在每一步的構造過程中可以引入參數,使得經過相同的步驟建立最後得到的對象的展示不一樣。

 

可以看到ImageBean的構造參數改成了這個builder,然後定義了一個和ImageBean一樣的Builder,傳回值都是builder對象。這就是定義裡面說的構造和表示分離,構造是ImageBean中來構造,而顯示則在Builder中顯示。也就是分布構造每一部分。

以上只是builder設計模式的一種用法總結:

在以下情況使用Build模式:

1 當建立複雜物件的演算法應該獨立於該對象的組成部分以及它們的裝配方式時。

2 當構造過程必須允許被構造的對象有不同的表示時。

3 Builder模式要解決的也正是這樣的問題:

  當我們要建立的對象很複雜的時候(通常是由很多其他的對象組合而成),

  我們要複雜物件的建立過程和這個對象的表示(展示)分離開來,

  這樣做的好處就是通過一步步的進行複雜物件的構建,

  由於在每一步的構造過程中可以引入參數,使得經過相同的步驟建立最後得到的對象的展示不一樣。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.