建造者模式与工厂模式的最大区别是,建造者模式更关注产品的组合方式和装配顺序,而工厂模式更关注产品的生产。
以生产电脑为类,电脑包括CPU,Memory,Disk等生产过程
具体实现如下:
1、定义需要生产的产品Computer
/** * 定义需要生产的产品Computer */ public class Computer { private String cpu; private String memory; private String disk; public String getCpu() { return cpu; } public void setCpu(String cpu) { this.cpu = cpu; } public String getMemory() { return memory; } public void setMemory(String memory) { this.memory = memory; } public String getDisk() { return disk; } public void setDisk(String disk) { this.disk = disk; } }
2、定义抽象接口ComputerBuilder描述产品构造和装配过程:
/** * 定义抽象接口Computer来描述产品构造和装配过程 */ public interface ComputerBuilder { //组装CPU void buildCpu(); //组装内存的方法 void buildMemory(); //组装磁盘 void buildDisk(); //以上组装完成后,就可以调用buildComputer()组装一台完整的电脑了 Computer buildComputer(); }
3、定义ComputerBuilder接口实现类
public class ComputerBuilderImpl implements ComputerBuilder { Computer computer; public ComputerBuilderImpl(){ computer = new Computer(); } @Override public void buildCpu() { System.out.println("build cpu"); computer.setCpu("8 core"); } @Override public void buildMemory() { System.out.println("build memory"); computer.setMemory("32G"); } @Override public void buildDisk() { System.out.println("build disk"); computer.setDisk("1024 GB"); } @Override public Computer buildComputer() { return computer; } }
4、定义ComputerDirector使用Builder接口实现产品的装配,该类是建造者模式对产品生产过程的封装,在需求化变时,如需更改装配顺序,调整这个类即可。
/** * 定义ComputerDirector使用Builder接口实现产口的装配 * 该类是建造者模式对产品生产的过程的封装,在需求变化时,如需先装磁盘再装CPU,只需调整Director的执行顺序即可,每个组件的装配都稳定不变。 */ public class ComputerDirector { public Computer constructComputer(ComputerBuilder computerBuilder){ computerBuilder.buildCpu(); computerBuilder.buildMemory(); computerBuilder.buildDisk(); return computerBuilder.buildComputer(); } }
5、构建Computer
/** * 构建Computer * 定义一个ComputerDirector和ComputerBuilder为构建Computer做准备 * 然后通过ComputerDirector的constructComputer()实现Computer的构建 * 构建者在JDK中的应用:StringBuilder */ public class Main { public static void main(String[] args) { ComputerDirector computerDirector = new ComputerDirector(); ComputerBuilder computerBuilder = new ComputerBuilderImpl(); Computer computer = computerDirector.constructComputer(computerBuilder); System.out.println(computer.getCpu()); System.out.println(computer.getMemory()); System.out.println(computer.getDisk()); StringBuilder sbBuilderPattern = new StringBuilder(); } }
7、建造者模式在JDK中的应用StringBuilder
StringBuilder源码解析
StringBuilder sbBuilderPattern = new StringBuilder(); public final class StringBuilder extends AbstractStringBuilder abstract class AbstractStringBuilder implements Appendable, CharSequence { /** * The value is used for character storage. */ char[] value; public interface Appendable { Appendable append(CharSequence csq) throws IOException; Appendable append(CharSequence csq, int start, int end) throws IOException; Appendable append(char c) throws IOException; }
分析:
Appendable 接口定义了多个append方法,即Appendable为抽象建造者
AbstractStringBuilder 实现了 Appendable 接口方法,这里 AbstractStringBuilder 已经是建造者,只是还不能实例化
StringBuilder 即充当了指挥者角色,也充当了具体的建造者,建造方法的实现由 AbstractStringBuilder 完成,而StringBuilder继承了AbstractStringBuilder