微信扫一扫打赏支持

设计模式四--建造者模式

设计模式四--建造者模式

一、定义

将一个复杂对象呢的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

 

二、优点

封装性

建造者独立,容易扩展

 

三、原理图

 

Product代表具体的产品

ConcreteBuilder是这些产品建造过程的一个接口

Builder是这些产品的具体建造实例,实现ConcreteBuilder接口

Director包含不同的Builder,负责构造各个产品

 

四、实例

1、Computer

抽象的计算机类

public abstract class Computer {
    private String type;//型号
    private String cpu;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCpu() {
        return cpu;
    }

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

 

2、T410

继承了Computer类

代表T410电脑

 1 package definition;
 2 
 3 public class T410 extends Computer {
 4     private String graphicCard;//显卡
 5     public T410(){
 6         this.setType("ThinkPad T410");
 7     }
 8     public String getGraphicCard() {
 9         return graphicCard;
10     }
11     public void setGraphicCard(String graphicCard) {
12         this.graphicCard = graphicCard;
13     }
14     
15     
16     //输出型号,内存和显卡
17     public String toString() {
18         return "T410 [getType()=" + getType() + ", getCpu()=" + getCpu() + ", graphicCard=" + graphicCard + "]";
19     }
20     
21 }

 

3、X201

继承了Computer类

代表X201电脑

 1 public class X201 extends Computer {
 2 
 3     public X201() {
 4         this.setType("ThinkPad X201");
 5     }
 6 
 7     //显示型号和cpu
 8     public String toString() {
 9         return "X201 [getType()=" + getType() + ", getCpu()=" + getCpu() + "]";
10     }
11 
12 }

 

4、ComputerBuilder

接口类,用于建造电脑

1 public interface ComputerBuilder {
2     void buildCpu();//建造cpu
3     void buildGraphicCard();//建造显卡
4     
5     Computer getResult();//得到建造好的计算机
6 }

 

5、T410Builder

实现ComputerBuilder接口

T410的具体实现

 1 public class T410Builder implements ComputerBuilder {
 2     private T410 computer =new T410();
 3 
 4     public void buildCpu() {
 5         computer.setCpu("i5-450");
 6     }
 7     
 8     public void buildGraphicCard(){
 9         computer.setGraphicCard("Nvidia NVS 3100M");
10     }
11 
12     public Computer getResult() {
13         
14         return computer;
15     }
16 
17 }

 

6、X201Builder

实现ComputerBuilder接口

X201的具体实现

public class X201Builder implements ComputerBuilder {

    private X201 computer = new X201();
    public void buildCpu() {
        computer.setCpu("i3-350");
    }

    public Computer getResult() {
        return computer;
    }

    public void buildGraphicCard() {
        // TODO Auto-generated method stub
        
    }

}

 

7、ComputerDirector

Director包含不同的Builder,负责构造各个产品

 1 public class ComputerDirector {
 2     ComputerBuilder builder;
 3 
 4     //建造T410号机器
 5     public T410 constructT410() {
 6         builder=new T410Builder();
 7         builder.buildCpu();
 8         builder.buildGraphicCard();
 9         return (T410)builder.getResult();
10     }
11 
12     //建造X201号机器
13     public X201 constructX201() {
14         builder=new X201Builder();
15         builder.buildCpu();
16         return (X201)builder.getResult();
17     }
18 
19 }

 

8、ComputerTest

具体的测试类

 

 1 public class ComputerTest {
 2 
 3     public static void main(String[] args) {
 4         ComputerDirector computerDirector=new ComputerDirector();
 5         Computer t410=computerDirector.constructT410();
 6         System.out.println(t410);
 7         
 8         System.out.println("----------------------------");
 9         
10         Computer x201=computerDirector.constructX201();
11         System.out.println(x201);
12     }
13 
14 }

 

五、实验结果

T410 [getType()=ThinkPad T410, getCpu()=i5-450, graphicCard=Nvidia NVS 3100M]

----------------------------

X201 [getType()=ThinkPad X201, getCpu()=i3-350]

posted @ 2017-05-23 21:25  范仁义  阅读(347)  评论(0编辑  收藏  举报