建造者模式
What
建造者模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
- 建造复杂的对象;
- 创建与表示分离;
- 同样构造过程可以创建不同表示;
- 一步一步构造最终的对象;
Builder:抽象建造者;
ConcreteBuilder:具体建造者,实现Builder,知道要建造什么对象,建造每一步的具体细节,但是不知道建造的过程(顺序);
Director:监工(导演/指挥/有多种叫法),知道建造的过程(顺序);
How
示例1:简单示例
//测试类
public class Test {
public static void main(String []args) {
//answer sentence
Builder answer = new SentenceBuilder();
Director director = new Director(answer);
director.construct("Answer");
System.out.println(answer.say());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//question sentence
Builder question = new SentenceBuilder();
Director director1 = new Director(question);
director1.construct("Question");
System.out.println(question.say());
}
}
//抽象builder
interface Builder{
void word_how();
void word_are();
void word_you();
void word_i();
void word_am();
void word_fine();
String say();
}
//句子builder:知道创建StringBuilder的每一个步骤,但不知道顺序
class SentenceBuilder implements Builder{
StringBuilder sb = new StringBuilder();
@Override
public void word_how(){
sb.append("how");
}
@Override
public void word_are(){
sb.append(" are");
}
@Override
public void word_you(){
sb.append(" you");
}
@Override
public void word_i(){
sb.append("I");
}
@Override
public void word_am(){
sb.append(" am");
}
@Override
public void word_fine(){
sb.append(" fine");
}
@Override
public String say(){
return sb.toString();
}
}
//Director知道怎么用builder去创建StringBuilder
class Director{
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void construct(String answerOrQuestion){
if ( "Question".equals(answerOrQuestion) ) {
builder.word_how();
builder.word_are();
builder.word_you();
} else if ( "Answer".equals(answerOrQuestion) ) {
builder.word_i();
builder.word_am();
builder.word_fine();
} else {
//null
}
}
}
示例2:建造复杂Pen对象
public class Test {
public static void main(String []args) {
//answer sentence
Builder penBuilder = new PenBuilder();
Director director = new Director(penBuilder);
director.construct();
System.out.println("pen : " + penBuilder.get());
}
}
class Pen{
private String color;
private Double price;
public void setColor( String color ){
this.color = color;
}
public String getColor(){
return this.color;
}
public void setPrice( Double price ){
this.price = price;
}
public Double getPrice(){
return this.price;
}
}
//抽象builder
interface Builder{
void initColor();
void initPrice();
void before();
void after();
Pen get();
}
//Pen Builder
class PenBuilder implements Builder{
private Pen pen;
public PenBuilder () {
this.pen = new Pen();
}
@Override
public void initColor(){
pen.setColor("Red");
System.out.println("set color");
}
@Override
public void initPrice(){
pen.setPrice(12.0);
System.out.println("set price");
}
@Override
public void before(){
System.out.println("begin");
}
@Override
public void after(){
System.out.println("done!");
}
@Override
public Pen get(){
return this.pen;
}
}
//Director知道怎么用builder去创建Pen
class Director{
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void construct(){
builder.before();
builder.initPrice();
builder.initColor();
builder.after();
}
}
示例3:建造者模式与链式调用结合
public class Test {
public static void main(String []args) {
Pen pen = new Pen.PenBuilder().setColor("Red").setPrice(12.0).build();
System.out.println("Pen:" + pen);
System.out.println("Color:" + pen.getColor());
System.out.println("Price:" + pen.getPrice());
}
}
class Pen {
private String color;
private Double price;
public Pen( PenBuilder builder ) {
color = builder.color;
price = builder.price;
}
public void setColor( String color ){
this.color = color;
}
public String getColor(){
return this.color;
}
public void setPrice( Double price ){
this.price = price;
}
public Double getPrice(){
return this.price;
}
public static class PenBuilder{
private String color;
private Double price;
public PenBuilder setColor( String color ){
this.color = color;
return this;
}
public PenBuilder setPrice( Double price ){
this.price = price;
return this;
}
public Pen build(){
return new Pen(this);
}
}
}
为什么没有Director了呢?
new Pen.PenBuilder().setColor("Red").setPrice(12.0).build()
就相当于Director
示例4:StringBuilder的append方法;
/**
* Constructs a string builder initialized to the contents of the
* specified string. The initial capacity of the string builder is
* {@code 16} plus the length of the string argument.
*
* @param str the initial contents of the buffer.
*/
public StringBuilder(String str) {
super(str.length() + 16);
append(str);
}
父类:AbstractStringBuilder
/**
* @since 1.8
*/
AbstractStringBuilder append(AbstractStringBuilder asb) {
if (asb == null)
return appendNull();
int len = asb.length();
ensureCapacityInternal(count + len);
asb.getChars(0, len, value, count);
count += len;
return this;
}
Q & A
建造者模式与模板模式的区别?
参考链接link
建造者模式与工厂模式的区别?
个人理解:
工厂模式更倾向于一次性创建一个对象,并一次性初始化参数等等,有点一步到位的意思;
而建造者模式,有点可定制化创建对象的意味在里面,或者说就是其中的一个特点。
优化:建造者模式与链式调用结合
相关链接link
More
结束语:优秀是一种习惯。