建造者模式
介绍
建造者模式是创建型模式之一,使用多个简单的对象创建一个复杂的对象,定义不同的构造类使得相同的构建过程可以创建不同的对象。同工厂模式不同,工厂模式重点关注的是对象的创建过程,建造者模式重点关注的是对象的组装过程。
模块
- Builder:构造者接口
- ConcreteBuilder:具体构造者,实现构造器接口,定义复杂对象的创建流程
- Product:被创建的对象
- Director:指导者,调用具体构造者创建对象的各个部分
场景
在需求开发中,当需要创建一个复杂对象时,需要对这个对象的各个子部件创建并组合。例如苹果电脑,我们不能自己组装电脑,而是在官网上下单,由专门的技术人员根据我们选择的配置进行组装。电脑里面的部件是固定数量的,但是各个部件的配置是不一样的。面临剧烈的需求变化,复杂对象的各个部件是会经常变化的,但是对象的各个结构是不变的
结构代码演示
接下来我们就以我手中 M1Pro 32G 1T 10核的MacBook Pro举例,嘿嘿。按照苹果官网有高中低三档配置,我使用高中低三个建造者进行电脑的各个组件的创建,然后让指导者进行组装
# 产品 macBook
public class MacBook {
private String memory;
private String slug;
private String cpu;
private String disk;
// 省略get set
}
# 建造者接口
import com.hello.designPattern.BuilderPattern.model.MacBook;
public interface MacBookBuilder {
MacBookBuilder buildMemory();
MacBookBuilder buildSlug();
MacBookBuilder buildCpu();
MacBookBuilder buildDisk();
MacBook build();
}
# 高中低 三个建造者
import com.hello.designPattern.BuilderPattern.interfaces.MacBookBuilder;
import com.hello.designPattern.BuilderPattern.model.MacBook;
public class HighMacBookBuilder implements MacBookBuilder {
private MacBook macBook = new MacBook();
@Override
public MacBookBuilder buildMemory() {
macBook.setMemory("安装32G运行内存");
return this;
}
@Override
public MacBookBuilder buildSlug() {
macBook.setSlug("安装M1X芯片");
return this;
}
@Override
public MacBookBuilder buildCpu() {
macBook.setCpu("安装10核中央处理器");
return this;
}
@Override
public MacBookBuilder buildDisk() {
macBook.setDisk("安装1TB固态硬盘");
return this;
}
public MacBook build(){
return macBook;
}
}
import com.hello.designPattern.BuilderPattern.interfaces.MacBookBuilder;
import com.hello.designPattern.BuilderPattern.model.MacBook;
public class MiddleMacBookBuilder implements MacBookBuilder {
private MacBook macBook;
@Override
public MacBookBuilder buildMemory() {
macBook.setMemory("安装16G运行内存");
return this;
}
@Override
public MacBookBuilder buildSlug() {
macBook.setSlug("安装M1Pro芯片");
return this;
}
@Override
public MacBookBuilder buildCpu() {
macBook.setCpu("安装10核中央处理器");
return this;
}
@Override
public MacBookBuilder buildDisk() {
macBook.setDisk("安装1TB固态硬盘");
return this;
}
public MacBook build(){
return macBook;
}
}
import com.hello.designPattern.BuilderPattern.interfaces.MacBookBuilder;
import com.hello.designPattern.BuilderPattern.model.MacBook;
public class LowMacBookBuilder implements MacBookBuilder {
private MacBook macBook = new MacBook();
@Override
public MacBookBuilder buildMemory() {
macBook.setMemory("安装16G运行内存");
return this;
}
@Override
public MacBookBuilder buildSlug() {
macBook.setSlug("安装M1Pro芯片");
return this;
}
@Override
public MacBookBuilder buildCpu() {
macBook.setCpu("安装10核中央处理器");
return this;
}
@Override
public MacBookBuilder buildDisk() {
macBook.setDisk("安装512G固态硬盘");
return this;
}
public MacBook build(){
return macBook;
}
}
# 指导者
import com.hello.designPattern.BuilderPattern.interfaces.MacBookBuilder;
import com.hello.designPattern.BuilderPattern.model.MacBook;
public class Director {
private MacBookBuilder macBookBuilder;
public Director(MacBookBuilder macBookBuilder) {
this.macBookBuilder = macBookBuilder;
}
public MacBook construct(){
return macBookBuilder.buildMemory().buildDisk().buildCpu().buildSlug().build();
}
}
测试方法
package com.hello.designPattern.singletonPattern;
import com.hello.designPattern.BuilderPattern.builder.HighMacBookBuilder;
import com.hello.designPattern.BuilderPattern.director.Director;
import com.hello.designPattern.BuilderPattern.model.MacBook;
import org.junit.Test;
public class BuilderPatternTest {
@Test
public void builderPatternTest(){
Director director = new Director(new HighMacBookBuilder());
MacBook macBook = director.construct();
System.out.println(macBook.toString());
}
}
总结
首先,建造者模式的封装性很好。使用建造者模式可以有效的封装变化,在使用建造者模式的场景中,一般产品类和建造者类是比较稳定的,因此,将主要的业务逻辑封装在导演类中对整体而言可以取得比较好的稳定性。
其次,建造者模式很容易进行扩展。如果有新的需求,通过实现一个新的建造者类就可以完成,基本上不用修改之前已经测试通过的代码,因此也就不会对原有功能引入风险。
参考链接
建造者模式(Java实现)
java实现建造者模式(Builder Pattern)
建造者模式
|