美文网首页
[Builder Pattern] 建造者模式

[Builder Pattern] 建造者模式

作者: Sinexs | 来源:发表于2016-07-19 23:01 被阅读0次

What does Builder Pattern looks like

In Material-Dialog

new MaterialDialog.Builder(this) 
             .title(R.string.title) 
             .content(R.string.content) 
             .positiveText(R.string.agree) 
             .negativeText(R.string.disagree) 
             .show();

In OkHttp

Request request = new Request.Builder() 
                         .url(url) 
                         .post(body) 
                         .build();

Also see in Android Source Code - AlertDialog, Notification...

What is Builder Pattern

The builder pattern is an object creation software design pattern.
首先,建造者(生成器)模式是一种构建对象的设计模式。

  • 它可以将复杂对象的建造过程抽象出来(抽象类别),分离对象的表示和构建, 使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象。

When use Builder Pattern

When would you use the Builder Pattern? [closed]

The builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters.

一般的构造方法

  • Telescoping Constructor Pattern
Pizza(int size) { ... } Pizza(int size, boolean cheese) { ... } 
Pizza(int size, boolean cheese, boolean pepperoni) { ... } 
Pizza(int size, boolean cheese, boolean pepperoni, boolean bacon) { ... }
  • parameters limitation
  • difficult to remember required order of the parameters
  • JavaBean Pattern
Pizza pizza = new Pizza(12);
pizza.setCheese(true);
pizza.setPepperoni(true);
pizza.setBacon(true);
  • The problem here is that because the object is created over several calls it may be in an inconsistent state partway through its construction. This also requires a lot of extra effort to ensure thread safety.

The better alternative is to use the Builder Pattern.

How to use Builder Pattern

public class Pizza { 
  private int size; 
  private boolean cheese; 
  private boolean pepperoni; 
  private boolean bacon; 

  private Pizza(Builder builder) { 
    size = builder.size; 
    cheese = builder.cheese; 
    pepperoni = builder.pepperoni; 
    bacon = builder.bacon; 
  }

  public static class Builder { 
    //required 
    private final int size; 
  
    //optional 
    private boolean cheese = false; 
    private boolean pepperoni = false; 
    private boolean bacon = false; 

    public Builder(int size) { 
      this.size = size; 
    } 

    public Builder cheese(boolean value) { 
      cheese = value; 
      return this; 
    } 

    public Builder pepperoni(boolean value) { 
      pepperoni = value; 
      return this; 
    } 

    public Builder bacon(boolean value) { 
      bacon = value; 
      return this; 
   } 

    public Pizza build() { 
      return new Pizza(this); 
    } 

  } 
}
  • Because the Builder's setter methods return the Builder object they are able to be chained.
Pizza pizza = new Pizza.Builder(12)
            .cheese(true) 
            .pepperoni(true) 
            .bacon(true) 
            .build();
  • This results in code that is easy to write and very easy to read and understand.
  • This pattern is flexible and it is easy to add more parameters to it in the future.It might be worthwhile in the first place if you suspect you may be adding more parameters in the future.

Conclusion

  • 大量参数
  • 良好的扩展性
  • 通用性
  • 造轮子

Reference

相关文章

网友评论

      本文标题:[Builder Pattern] 建造者模式

      本文链接:https://www.haomeiwen.com/subject/zgxvjttx.html