标签:
建造者设计模式在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
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.
(能力至此,实在不懂)
我们将会使用相同的例子通过使用Builder design pattern,
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 + ‘}‘; }
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在创建对象时。
优点:
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://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
标签:
原文地址:http://www.cnblogs.com/ryan-ys/p/4780389.html