建造者模式也是属于创建型模式之一,其作用是使用多个简单的对象一步一步构建成一个复杂的对象,它提供了一种创建对象的最佳方式。
模式名称 | 优点 | 缺点 | 应用场景 |
建造者模式 |
1、建造者独立,易扩展。 2、便于控制细节风险。 |
1、产品必须有共同点,范围有限制。 2、如内部变化复杂,会有很多的建造类。 |
1.生成的对象很复杂,构造方法参数多,且部分是可选的 2.需要生成的对象内部属性本身相互依赖 |
注意事项:与工厂模式的区别是:建造者模式更加关注与零件装配的顺序。
下面就以计算机组装举例:
简化版
创建一个Computer类,类种包含一个静态内部类,如下所示:
public class Computer { private String cpu;//必须 private String ram;//必须 private int usbCount;//可选 private String keyboard;//可选 private String display;//可选 private Computer(Builder builder) { this.cpu = builder.cpu; this.ram = builder.ram; this.usbCount = builder.usbCount; this.keyboard = builder.keyboard; this.display = builder.display; } public static class Builder { private String cpu;//必须 private String ram;//必须 private int usbCount;//可选 private String keyboard;//可选 private String display;//可选 public Builder(String cpu, String ram) { this.cpu = cpu; this.ram = ram; } public Builder setUsbCount(int usbCount) { this.usbCount = usbCount; return this; } public Builder setKeyboard(String keyboard) { this.keyboard = keyboard; return this; } public Builder setDisplay(String display) { this.display = display; return this; } public Computer builder() { return new Computer(this); } } @Override public String 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('}'); return sb.toString(); } }
测试方法:
public static void main(String[] args) { // 建造正模式 Computer computer = new Computer.Builder("因特尔", "三星") .setDisplay("三星24寸") .setKeyboard("罗技") .setUsbCount(2) .builder(); System.out.println(computer); } }
传统建造者模式
第一步:我们的目标Computer类:
public class Computer { private String cpu;//必须 private String ram;//必须 private int usbCount;//可选 private String keyboard;//可选 private String display;//可选 public Computer(String cpu, String ram) { this.cpu = cpu; this.ram = ram; } public void setUsbCount(int usbCount) { this.usbCount = usbCount; } public void setKeyboard(String keyboard) { this.keyboard = keyboard; } public void setDisplay(String display) { this.display = display; } @Override public String toString() { return "Computer{" + "cpu='" + cpu + '\'' + ", ram='" + ram + '\'' + ", usbCount=" + usbCount + ", keyboard='" + keyboard + '\'' + ", display='" + display + '\'' + '}'; } }
第二步:抽象构建者类
public abstract class ComputerBuilder { public abstract void setUsbCount(); public abstract void setKeyboard(); public abstract void setDisplay(); public abstract Computer getComputer(); }
第三步:实体构建者类,根据要构建的产品种类产生多个实体构建者类
①苹果电脑构建者类
public class MacComputerBuilder extends ComputerBuilder { private Computer computer; public MacComputerBuilder(String cpu, String ram) { computer = new Computer(cpu, ram); } @Override public void setUsbCount() { computer.setUsbCount(2); } @Override public void setKeyboard() { computer.setKeyboard("苹果键盘"); } @Override public void setDisplay() { computer.setDisplay("苹果显示器"); } @Override public Computer getComputer() { return computer; } }
②联想电脑构建者类
public class LenovoComputerBuilder extends ComputerBuilder { private Computer computer; public LenovoComputerBuilder(String cpu, String ram) { computer=new Computer(cpu,ram); } @Override public void setUsbCount() { computer.setUsbCount(4); } @Override public void setKeyboard() { computer.setKeyboard("联想键盘"); } @Override public void setDisplay() { computer.setDisplay("联想显示器"); } @Override public Computer getComputer() { return computer; } }
第四步:指导者类(Director)
public class ComputerDirector { public void makeComputer(ComputerBuilder builder){ builder.setUsbCount(); builder.setDisplay(); builder.setKeyboard(); } }
测试
public static void main(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='联想显示器'}
个人学习随笔,若是帮助到您,请给个推荐,或者评论一下下,谢谢了哟。若有不懂之处,请联系QQ:351094262(验证信息:博客园),原创不易,转载请标明出处!