1 模式定义
构建器模式(Builder Pattern):将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
2 模式结构
建造者模式包含如下角色:
Builder:抽象建造者
ConcreteBuilder:具体建造者
Director:指挥者
Product:产品角色
结构图
以下是一个简单的
HUAWEI MatePad Pro
生产过程演示
3 代码展示
抽象建造者(HuaWeiBuilder)
/*
* @Description: 抽象构建器
* @Author: weh
* @Date: 2024/3/20 21:33
*/
public interface HuaWeiBuilder {
//构建自组建
Battery builderBattery();
Screen builderScreen();
CPU builderCPU();
}
具体建造者(MyHuaWeiBuilder)
public class MyHuaWeiBuilder implements HuaWeiBuilder{
@Override
public Battery builderBattery() {
System.out.println("构建电池");
return new Battery("构建华为平板的电池");
}
@Override
public Screen builderScreen() {
// TODO Auto-generated method stub
System.out.println("构建屏幕");
return new Screen("构建华为平板的屏幕");
}
@Override
public CPU builderCPU() {
System.out.println("构建CPU");
return new CPU("构建华为平板的CPU");
}
}
抽象指挥者(HUAWEI_Pad_Director)
public interface HUAWEI_Pad_Director {
HUAWEI_Mate_Pad_Pro dircectHuaWei();
}
具体指挥者(MyHUAWEI_Pad_Director)
/*
* @Description: 指挥者实现类
* @Author: weh
* @Date: 2024/3/20 21:34
*/
public class MyHUAWEI_Pad_Director implements HUAWEI_Pad_Director {
private MyHuaWeiBuilder builder;
public MyHUAWEI_Pad_Director(MyHuaWeiBuilder builder) {
this.builder = builder;
}
@Override
public HUAWEI_Mate_Pad_Pro dircectHuaWei() {
//配件
Battery battery = builder.builderBattery();
Screen screen = builder.builderScreen();
CPU cpu = builder.builderCPU();
//装配华为平板
HUAWEI_Mate_Pad_Pro huawei_mate_pad_pro = new HUAWEI_Mate_Pad_Pro();
huawei_mate_pad_pro.setBattery(battery);
huawei_mate_pad_pro.setScreen(screen);
huawei_mate_pad_pro.setCpu(cpu);
return huawei_mate_pad_pro;
}
}
实体类
package com.weh.entity;
public class HUAWEI_Mate_Pad_Pro {
private Battery battery; //电池
private Screen screen; //显示屏
private CPU cpu; //CPU
public void combination() {
System.out.println("----------构建完成一台华为平板------------");
}
public void setBattery(Battery battery) {
this.battery = battery;
}
public void setScreen(Screen screen) {
this.screen = screen;
}
public void setCpu(CPU cpu) {
this.cpu = cpu;
}
}
/*
* @Description: 电池实体类
* @Author: weh
* @Date: 2024/3/20 21:54
*/
public class Battery {
private String name;
public Battery(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* @Description: 屏幕实体类
* @Author: weh
* @Date: 2024/3/20 21:53
*/
public class Screen {
private String name;
public Screen(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* @Description: CPU实体类
* @Author: weh
* @Date: 2024/3/20 21:54
*/
public class CPU {
private String name;
public CPU(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
测试类
/*
* @Description: 测试类
* @Author: weh
* @Date: 2024/3/20 21:53
*/
public class TestHUAWEI_MATE_PAD {
public static void main(String[] args) {
HUAWEI_Pad_Director director = new MyHUAWEI_Pad_Director(new MyHuaWeiBuilder());
HUAWEI_Mate_Pad_Pro huawei_mate_pad_pro = director.dircectHuaWei();
huawei_mate_pad_pro.combination();
}
}
4 结果展示
5 使用场景
- 需要生成的产品对象有复杂的内部结构,这些产品对象通常包含多个成员属性。
- 需要生成的产品对象的属性相互依赖,需要指定其生成顺序。
- 对象的创建过程独立于创建该对象的类。在建造者模式中引入了指挥者类,将创建过程封装在指挥者类中,而不在建造者类中。
- 隔离复杂对象的创建和使用,并使得相同的创建过程可以创建不同的产品。