首页 > 代码库 > 设计模式 -- 建造者模式

设计模式 -- 建造者模式

概念                                                                                         

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

类图                                                                                          

1

代码                                                                                          

public abstract class Computer {    private String type;    private String cpu;    private String ram;    private String os;    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;    }    public String getRam() {        return ram;    }    public void setRam(String ram) {        this.ram = ram;    }    public String getOs() {        return os;    }    public void setOs(String os) {        this.os = os;    }    }

创建两种型号的计算机:

public class T410 extends Computer {    private String hardDisk;    public T410() {        this.setType("ThinkPad T410i");    }    public String getHardDisk() {        return hardDisk;    }    public void setHardDisk(String hardDisk) {        this.hardDisk = hardDisk;    }    @Override    public String toString() {        return "T410 [hardDisk=" + hardDisk + ", getType()=" + getType()                + ", getCpu()=" + getCpu() + ", getRam()=" + getRam()                + ", getOs()=" + getOs() + ", getClass()=" + getClass()                + ", hashCode()=" + hashCode() + "]";    }}
public class X201 extends Computer {    public X201() {        this.setType("Thinkpad X201i");    }    @Override    public String toString() {        return "X201 [getType()=" + getType() + ", getCpu()=" + getCpu()                + ", getRam()=" + getRam() + ", getOs()=" + getOs()                + ", getClass()=" + getClass() + ", hashCode()=" + hashCode()                + "]";    }}

在计算机产品类的基础上增加一个ComputerBuilder接口,以及两个的实现类,以便对计算机进行生产:

public interface ComputerBuilder {    void buildCpu();    void buildRam();    void buildHardDisk();    void BuildOs();        Computer getResult();}
public class T410Builder implements ComputerBuilder {    private T410 computer = new T410();    @Override    public void buildCpu() {        computer.setCpu("i5");    }    @Override    public void buildRam() {        computer.setRam("4G");    }    @Override    public void buildHardDisk() {        computer.setHardDisk("500G");    }    @Override    public void BuildOs() {        computer.setOs("Win7");    }    @Override    public Computer getResult() {        return computer;    }}
public class X201Builder implements ComputerBuilder {    private X201 computer = new X201();        @Override    public void buildCpu() {        computer.setCpu("i7");    }    @Override    public void buildRam() {        computer.setRam("8G");    }    @Override    public void buildHardDisk() {        //没有HaedDisk    }    @Override    public void BuildOs() {        computer.setOs("Win8");    }    @Override    public Computer getResult() {        return computer;    }}

再鞥家导演者:

public class ComputerDirector {    ComputerBuilder builder;    public T410 constructT410()    {        builder = new T410Builder();        builder.buildCpu();        builder.buildHardDisk();        builder.BuildOs();        builder.buildRam();        return (T410) builder.getResult();    }        public X201 constructX201()    {        builder = new X201Builder();        builder.buildCpu();        builder.buildHardDisk();        builder.BuildOs();        builder.buildRam();        return (X201) builder.getResult();    }}

测试:

public class ComputerTest {    public static void main(String[] args) {        ComputerDirector director = new ComputerDirector();        Computer t410 = director.constructT410();        System.out.println(t410);        System.out.println("-------------------------");        Computer x201 = director.constructX201();        System.out.println(x201);    }}

与抽象工厂的区别                                                                        

在建造者模式里,有个指导者,由指导者来管理建造者,用户是与指导者联系的,指导者联系建造者最后得到产品。即建造模式可以强制实行一种分步骤进行的建造过程。

建造模式是将复杂的内部创建封装在内部,对于外部调用的人来说,只需要传入建造者和建造工具,对于内部是如何建造成成品的,调用者无需关心。

我是天王盖地虎的分割线                                                               

image

源代码:http://pan.baidu.com/s/1dD1Qx01

Java建造者.zip