java建造者模式_JAVA设计模式(四)——建造者模式

建造者模式也是属于创建型模式之一,其作用是使用多个简单的对象一步一步构建成一个复杂的对象,它提供了一种创建对象的最佳方式。

模式名称

优点

缺点

应用场景

建造者模式

1、建造者独立,易扩展。

2、便于控制细节风险。

1、产品必须有共同点,范围有限制。

2、如内部变化复杂,会有很多的建造类。

1.生成的对象很复杂,构造方法参数多,且部分是可选的

2.需要生成的对象内部属性本身相互依赖

注意事项:与工厂模式的区别是:建造者模式更加关注与零件装配的顺序。

下面就以计算机组装举例:

简化版

创建一个Computer类,类种包含一个静态内部类,如下所示:

public classComputer {private String cpu;//必须

private String ram;//必须

private int usbCount;//可选

private String keyboard;//可选

private String display;//可选

privateComputer(Builder builder) {this.cpu =builder.cpu;this.ram =builder.ram;this.usbCount =builder.usbCount;this.keyboard =builder.keyboard;this.display =builder.display;

}public static classBuilder {private String cpu;//必须

private String ram;//必须

private int usbCount;//可选

private String keyboard;//可选

private String display;//可选

publicBuilder(String cpu, String ram) {this.cpu =cpu;this.ram =ram;

}public Builder setUsbCount(intusbCount) {this.usbCount =usbCount;return this;

}publicBuilder setKeyboard(String keyboard) {this.keyboard =keyboard;return this;

}publicBuilder setDisplay(String display) {this.display =display;return this;

}publicComputer builder() {return new Computer(this);

}

}

@OverridepublicString toString() {final StringBuilder sb = new StringBuilder("Computer{");

sb.append("cpu='").append(cpu).append('\'');

sb.append(", ram='").append(ram).append('\'');

sb.append(", usbCount=").append(usbCount);

sb.append(", keyboard='").append(keyboard).append('\'');

sb.append(", display='").append(display).append('\'');

sb.append('}');returnsb.toString();

}

}

测试方法:

public static voidmain(String[] args) {//建造正模式

Computer computer = new Computer.Builder("因特尔", "三星")

.setDisplay("三星24寸")

.setKeyboard("罗技")

.setUsbCount(2)

.builder();

System.out.println(computer);

}

}

传统建造者模式

第一步:我们的目标Computer类:

public classComputer {private String cpu;//必须

private String ram;//必须

private int usbCount;//可选

private String keyboard;//可选

private String display;//可选

publicComputer(String cpu, String ram) {this.cpu =cpu;this.ram =ram;

}public void setUsbCount(intusbCount) {this.usbCount =usbCount;

}public voidsetKeyboard(String keyboard) {this.keyboard =keyboard;

}public voidsetDisplay(String display) {this.display =display;

}

@OverridepublicString toString() {return "Computer{" +

"cpu='" + cpu + '\'' +

", ram='" + ram + '\'' +

", usbCount=" + usbCount +

", keyboard='" + keyboard + '\'' +

", display='" + display + '\'' +

'}';

}

}

第二步:抽象构建者类

public abstract classComputerBuilder {public abstract voidsetUsbCount();public abstract voidsetKeyboard();public abstract voidsetDisplay();public abstractComputer getComputer();

}

第三步:实体构建者类,根据要构建的产品种类产生多个实体构建者类

①苹果电脑构建者类

public class MacComputerBuilder extendsComputerBuilder {privateComputer computer;publicMacComputerBuilder(String cpu, String ram) {

computer= newComputer(cpu, ram);

}

@Overridepublic voidsetUsbCount() {

computer.setUsbCount(2);

}

@Overridepublic voidsetKeyboard() {

computer.setKeyboard("苹果键盘");

}

@Overridepublic voidsetDisplay() {

computer.setDisplay("苹果显示器");

}

@OverridepublicComputer getComputer() {returncomputer;

}

}

②联想电脑构建者类

public class LenovoComputerBuilder extendsComputerBuilder {privateComputer computer;publicLenovoComputerBuilder(String cpu, String ram) {

computer=newComputer(cpu,ram);

}

@Overridepublic voidsetUsbCount() {

computer.setUsbCount(4);

}

@Overridepublic voidsetKeyboard() {

computer.setKeyboard("联想键盘");

}

@Overridepublic voidsetDisplay() {

computer.setDisplay("联想显示器");

}

@OverridepublicComputer getComputer() {returncomputer;

}

}

第四步:指导者类(Director)

public classComputerDirector {public voidmakeComputer(ComputerBuilder builder){

builder.setUsbCount();

builder.setDisplay();

builder.setKeyboard();

}

}

测试

public static voidmain(String[] args) {

ComputerDirector director=new ComputerDirector();//1

ComputerBuilder builder=new MacComputerBuilder("I5处理器","三星125");//2

director.makeComputer(builder);//3

Computer macComputer=builder.getComputer();//4

System.out.println("mac computer:"+macComputer.toString());

ComputerBuilder lenovoBuilder=new LenovoComputerBuilder("I7处理器","海力士222");

director.makeComputer(lenovoBuilder);

Computer lenovoComputer=lenovoBuilder.getComputer();

System.out.println("lenovo computer:"+lenovoComputer.toString());

}

输出结果

mac computer:Computer{cpu='I5处理器', ram='三星125', usbCount=2, keyboard='苹果键盘', display='苹果显示器'}

lenovo computer:Computer{cpu='I7处理器', ram='海力士222', usbCount=4, keyboard='联想键盘', display='联想显示器'}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值