你的位置:首页 > 操作系统

[操作系统]Android自学历程—Builder()模式


  前一篇文章,在学习OKHttp的时候遇到Builder pattern,当然那时候还不知道这是Builder模式,只是觉得奇怪怎么后面跟了好多个点,后来通过了解才明白这是Android 中的建造者模式。稍微学习过android的,一定用过AlertDialog.buider,说来惭愧!当时没写博客,也就局限于会用的阶层。通过这篇,希望能更加的了解。

 

Builder Design pattern in Java

  建造者设计模式在Java中是一种创造类型的模式。例如,用来创建对象,类似如Factory method design pattern ,她也是一种创造型设计模式。在我们学习任何一种设计模式的时候,我建议寻找到这种设计模式所解决的实际的问题。Its been well said necessity is mother on invention.(神句子,大意是因为需求所以被创造)。脱离实际的问题而学习设计模式是没有作用的,相反的,如果你已经遇到了问题那么你会更加的了解设计模式并且能够学会如何解决问题。在此Java设计模式的教程中,我们首先将会了解Builder design pattern所解决的实际的问题,这将给我们一些启示,我们该什么时候使用建造者模式。之后我们还会看见一下实例去了解建造者模式的优缺点。

 

 

What problem Builder pattern solves in Java

正如我之前所说的,Builder pattern 是一种创造型模式,这就意味的他所解决的问题与对象的创建有关。Java中的构造函数通常被用来创建对象,并且可以携带创建对象所需要的参数。问题就在于当创建一个对象可以有很多个参数时,一些参数可能是强制性的还有一些可能是可选的。我们想像一下有一个类,这个类是被用来创建蛋糕的,现在你需要去记着一些参数例如,鸡蛋,牛奶,面粉的比例去创建一个蛋糕。其中的一些是强制性的,还有一些是可选的例如,cherry,fruits。如果我们为每一个不同的对象都去重载构造函数,那势必会有好多好多的构造函数,并且最坏的就是他们会有好多好多的参数。

problem:

1)需要太多的构造函数

2)因为太多的字段所以容易出错。例如,

 sugar and and butter are in cups so instead of 2 cup sugar if you pass 2 cup butter, your compiler will not complain but will get a buttery cake with almost no sugar with high cost of wasting butter.

(能力至此,实在不懂)

 

 

Example of Builder Design pattern in Java

我们将会使用相同的例子通过使用Builder design pattern,

 

Guidelines for Builder design pattern in Java

1.创建一个叫做 Builder 的静态内部类(a static nested class),这这个类里面的对象将会通过Builder被创建。在这个例子里面对象是Cake。

2.

3.

4.

5.

 

 

BuilderPatternExample.java

public class BuilderPatternExample {    public static void main(String arg[]) {      //Creating object using Builder pattern in java      Cake whiteCake = new Cake.Builder()                          .sugar(1)                          .button(0.5)                          .egg(2)                          .vanila(2)                          .flour(1.5)                          .bakingpowder(0.75)                          .milk(0.5)                          .builder();      //Cake is ready to eat :)      System.out.printIn(whiteCake);    }}

 

 

Cake.class

Class Cake{  private final double sugar;  //cup  private final double butter;  //cup  private final int eggs;        private final int vanila;      //spoon  private final double flour;    //cup  private final double bakingpowder  //spoon  private final double milk;    //cup  private final int cherry;  public static class Builder {    private double sugar;   //cup    private double butter;  //cup    private int eggs;          private int vanila;     //spoon    private double bakingpoeder  //spoon    private double milk;    //spoon    private int cherry;    //builder methods for setting property    public Bulider sugar(double cup){this.sugar = cup; return this;}    public Builder butter(double cup){this.butter = cup; return this; }    public Builder eggs(int number){this.eggs = number; return this; }    public Builder vanila(int spoon){this.vanila = spoon; return this; }    public Builder flour(double cup){this.flour = cup; return this; }    public Builder bakingpowder(double spoon){this.sugar = spoon; return this; }    public Builder milk(double cup){this.milk = cup; return this; }    public Builder cherry(int number){this.cherry = number; return this; }    //return fully build object    public Cake build() {      return new Cake(this);    }        //private constructor to enforce object creation through builder  private Cake(Builder builder) {    this.sugar = builder.sugar;    this.butter = builder.butter;    this.eggs = builder.eggs;    this.vanila = builder.vanila;    this.flour = builder.flour;    this.bakingpowder = builder.bakingpowder;    this.milk = builder.milk;    this.cherry = builder.cherry;  }@Override  public String toString() {    return "Cake{" + "sugar=" + sugar + ", butter=" + butter + ", eggs=" + eggs + ", vanila=" + vanila + ", flour=" + flour + ", bakingpowder=" + bakingpowder + ", milk=" + milk + ", cherry=" + cherry + '}'; }

 

 

output:

Cake{sugar=0.75, butter=0.5, eggs=2, vanila=2, flour=1.5, bakingpowder=0.0, milk=0.5, cherry=0}

 

 

Builder design pattern in Java – Pros and Cons(优缺点)

构造者模式也有缺点,但是相对于优点还是很多的。如论如何都有一些优点和缺点关于 Builder design pattern在创建对象时。

优点:

1.更加的可维护 如果创建的对象需要的字段数(参数)超过4~5个

2.更少的出错

3.更加健壮,只有完整的构造函数能够被客户端得到

缺点:

1.冗长的代码重复,建设者需要从原始副本各领域或项目类复制

 

When to use Builder Design pattern in Java

建造者模式是一种创造型的模式并且应该用来当构造函数的参数数量多余易管理的4~5个。不要混淆Builder 和 Factory模式,她们之间有着明显的区别。

as Factory can be used to create different implementation of same interface but Builder is tied up with its Container class and only returns object of Outer class.

 

更多详情:

译文:http://www.javacodegeeks.com/2012/07/builder-design-pattern-in-java.html

 

 

http://www.javacodegeeks.com/2012/07/builder-design-pattern-in-java.html

http://jlordiales.me/2012/12/13/the-builder-pattern-in-practice/

http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern

 

android builder pattern