Posts 【创建型】建造者模式详解
Post
Cancel

【创建型】建造者模式详解

【创建型】建造者模式

定义


1
2
3
4
5
6
7
8
9
10
11
12
定义:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

建造者模式的四个要点:

    1.产品类,产品类可以是具体的类,也可以是抽象类与其实现类组成;
    2.抽象建造者,可以是个抽象类或者接口,一般有创建产品方法和返回产品方法;
    3.建造者,实现抽象建造者;
    4.导演类,调用建造者来得到具体产品;

建造者模式优点:
    1.建造者模式可以有效的封装变化,将业务逻辑封闭在导演类中,整体稳定性好;
    2.扩展性好,有新的需求,加入新的建造者即可;

UML图


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 _ _ _ _ _ _       _ _ _ _ _ _ _ _ _
|           |     |                 |
|  Director |----→| abstractBuilder |
|_ _ _ _ _ _|     |_ _ _ _ _ _ _ _ _|
                            ↑
                   _ _ _ _ _|_ _ _ _
                  |                 |
                  | concreteBuilder |
                  |_ _ _ _ _ _ _ _ _|
                            |
                            |
                   _ _ _ _ _↓_ _ _ _
                  |                 |
                  |     Product     |
                  |_ _ _ _ _ _ _ _ _|

代码


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class ComputerProduct{

    private String cpu;

    private String monitor;

    public void setCpu(String cpu){
        this.cpu = cpu;
    }

    public void setMonitor(String monitor){
        this.monitor = monitor;
    }

    public void show(String computerName){

        System.out.println( "We use "+cpu+" and "+monitor+" produce a computer called: "+computerName );
    }

}

1
2
3
4
5
6
7
public interface Builder{

    public void createProduct();

    public ComputerProduct getProduct();

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class BuilderX implements Builder{

    private computerProduct = new ComputerProduct();

    public void createProduct(String cpu, String monitor){

        computerProduct.setCpu( cpu );
        computerProduct.setMonitor( monitor );

    }

    public ComputerProduct getProduct(){
        return computerProduct;
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Director{

    private builder = new BuilderX();

    public ComputerProduct getProductDell(){

            builder.createProduct("intel","Samsung");
            return builder.getProduct();
    }

    public ComputerProduct getProductMac(){

            builder.createProduct("AMD","HP");
            return builder.getProduct();
    }

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Client{

    public static void main(String[] args){

        Director director = new Director();

        ComputerProduct Dell = director.getProductDell();

        Dell.show("Dell");


        ComputerProduct Mac = director.getProductMac();

        Mac.show("Mac");

    }
}

运行结果:

    We use intel and Samsung produce a computer called: Dell

    We use AMD and HP produce a computer called: Mac
    ---------------------------------------------------------

应用


JDK中建造者模式的的应用:

java.lang.StringBuilder#append()

java.lang.StringBuffer#append()

java.sql.PreparedStatement

javax.swing.GroupLayout.Group#addComponent()

This post is licensed under CC BY 4.0 by the author.

【创建型】抽象工厂模式详解

【创建型】原型模式详解