建造者模式(Builder Pattern)是一种创建型设计模式,它可以将复杂对象的构建过程与其表示分离,使得同样的构建过程可以创建不同的表示。该模式将对象的构建过程分解为多个简单的步骤,并将这些步骤按照特定的顺序进行组合,从而构建出不同的对象。
在Java中,建造者模式通常包含以下几个角色:
使用建造者模式创建一份电脑配置清单:
package com.sin.pojo;
/**
* @createTime 2023/12/21 9:40
* @createAuthor SIN
* @use
*/
public class Computer {
// cpu
private String cpu;
// 内存
private String memory;
// 磁盘
private String hardDisk;
public void setCpu(String cpu) {
this.cpu = cpu;
}
public void setMemory(String memory) {
this.memory = memory;
}
public void setHardDisk(String hardDisk) {
this.hardDisk = hardDisk;
}
public String toString() {
return "电脑: CPU=" + cpu + ", 内存=" + memory + ", 磁盘=" + hardDisk;
}
}
package com.sin.builder;
import com.sin.pojo.Computer;
/**
* @createTime 2023/12/21 9:45
* @createAuthor SIN
* @use 抽象建造者类
*/
public abstract class ComputerBuilder {
protected Computer computer;
// 创建CPU
public abstract void buildCpu();
// 创建内存
public abstract void buildMemory();
// 创建磁盘
public abstract void buildHardDisk();
public void createComputer(){
computer = new Computer();
}
public Computer getComputer(){
return computer;
}
}
package com.sin.director;
import com.sin.builder.ComputerBuilder;
/**
* @createTime 2023/12/21 9:53
* @createAuthor SIN
* @use 指挥者类
*/
public class ComputerDirector {
private ComputerBuilder computerBuilder;
public ComputerDirector(ComputerBuilder computerBuilder) {
this.computerBuilder = computerBuilder;
}
public void constructComputer(){
computerBuilder.createComputer();
computerBuilder.buildCpu();
computerBuilder.buildMemory();
computerBuilder.buildHardDisk();
}
}
package com.sin.builder;
/**
* @createTime 2023/12/21 9:50
* @createAuthor SIN
* @use 具体建造者
*/
public class HighEndComputerBuilder extends ComputerBuilder{
@Override
public void buildCpu() {
computer.setCpu("英特尔酷睿i9");
}
@Override
public void buildMemory() {
computer.setMemory("32GB");
}
@Override
public void buildHardDisk() {
computer.setHardDisk("1TB");
}
}
package com.sin.builder;
/**
* @createTime 2023/12/21 9:52
* @createAuthor SIN
* @use
*/
public class MidRangeComputerBuilder extends ComputerBuilder{
@Override
public void buildCpu() {
super.computer.setCpu("英特尔酷睿i5");
}
@Override
public void buildMemory() {
super.computer.setMemory("16GB");
}
@Override
public void buildHardDisk() {
super.computer.setHardDisk("500GB");
}
}
package com.sin.builder;
/**
* @createTime 2023/12/21 10:04
* @createAuthor SIN
* @use
*/
public class SuperComputerBuilder extends ComputerBuilder{
@Override
public void buildCpu() {
super.computer.setCpu("AMD 锐龙5 5600G");
}
@Override
public void buildMemory() {
super.computer.setMemory("128GB");
}
@Override
public void buildHardDisk() {
super.computer.setHardDisk("10TB");
}
}
package com.sin.demo;
import com.sin.builder.ComputerBuilder;
import com.sin.builder.HighEndComputerBuilder;
import com.sin.builder.MidRangeComputerBuilder;
import com.sin.builder.SuperComputerBuilder;
import com.sin.director.ComputerDirector;
import com.sin.pojo.Computer;
/**
* @createTime 2023/12/21 9:55
* @createAuthor SIN
* @use
*/
public class BuilderPatternDemo {
public static void main(String[] args) {
ComputerDirector director;
System.out.println("----------------高端电脑----------------");
ComputerBuilder highEndBuilder = new HighEndComputerBuilder();
director = new ComputerDirector(highEndBuilder);
director.constructComputer();
Computer highEndComputer = highEndBuilder.getComputer();
System.out.println("高端电脑配置信息:"+highEndComputer);
System.out.println("----------------低端电脑----------------");
ComputerBuilder midRangeBuilder = new MidRangeComputerBuilder();
director= new ComputerDirector(midRangeBuilder);
director.constructComputer();
Computer midRangeComputer = midRangeBuilder.getComputer();
System.out.println("低端电脑配置信息:"+midRangeComputer);
System.out.println("-------------------超级电脑---------------");
ComputerBuilder superComputerBuilder = new SuperComputerBuilder();
director= new ComputerDirector(superComputerBuilder);
director.constructComputer();
Computer superComputer = superComputerBuilder.getComputer();
System.out.println("超级电脑配置信息:"+superComputer);
}
}
优点:
缺点: