JAVA设计模式——(十一)建造者模式(Builder Pattern)

JAVA设计模式——(十一)建造者模式(Builder Pattern)

介绍

建造者模式用于将对象的创建和表示进行分离,即对象创建的过程单独提取出来,作为建造者的职能,使得同样的构建过程可以创建不同的表示。

理解

这么一读,是不是觉得建造者模式就是一条流水线似的。我们可以对比成一栋房子的建设,房子需要建造每一层,不管什么类型的房子,别墅、平层还是木头房子都是一层一层盖的,所以可以抽取整个建设过程的框架,而细节在由具体的建造者决定。

实现

Phone类

抽象类,用于提取框架。

package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/5/5 18:11
 * @Contact 643947568@qq.com
 */
public abstract class Phone {
    private String cpu;
    private String network;
    private String battery;
    // ...


    public String getBattery() {
        return battery;
    }

    public void setBattery(String battery) {
        this.battery = battery;
    }

    public String getCpu() {
        return cpu;
    }

    public void setCpu(String cpu) {
        this.cpu = cpu;
    }

    public String getNetwork() {
        return network;
    }

    public void setNetwork(String network) {
        this.network = network;
    }

    @Override
    public String toString() {
        return "Phone{" +
                "battery='" + battery + '\'' +
                ", cpu='" + cpu + '\'' +
                ", network='" + network + '\'' +
                '}';
    }
}

具体手机类

package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/5/5 18:14
 * @Contact 643947568@qq.com
 */
public class Android extends Phone{
    private String os;

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    @Override
    public String toString() {
        return "Android{" +
                "os='" + os + '\'' +
                "} " + super.toString();
    }
}

package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/5/5 18:15
 * @Contact 643947568@qq.com
 */
public class Apple extends Phone{
    private String os;

    public String getOs() {
        return os;
    }

    public void setOs(String os) {
        this.os = os;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "os='" + os + '\'' +
                "} " + super.toString();
    }
}

建造者

package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/5/5 18:16
 * @Contact 643947568@qq.com
 */
public interface PhoneBuilder {

    void buildCpu();

    void buildOs();

    void buildNetwork();

    void buildBattery();

    Phone getPhone();

}

具体建造者:

package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/5/5 18:17
 * @Contact 643947568@qq.com
 */
public class AndroidBuilder implements PhoneBuilder {

    private Android android = new Android();

    @Override
    public void buildCpu() {
        android.setCpu("骁龙8888");
    }

    @Override
    public void buildOs() {
        android.setOs("安卓100");
    }

    @Override
    public void buildNetwork() {
        android.setNetwork("双卡双待");
    }

    @Override
    public void buildBattery() {
        android.setBattery("10000mAh");
    }

    @Override
    public Phone getPhone() {
        return android;
    }
}

package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/5/5 18:17
 * @Contact 643947568@qq.com
 */
public class AppleBuilder implements PhoneBuilder {

    private Apple apple = new Apple();

    @Override
    public void buildCpu() {
        apple.setCpu("苹果处理器");
    }

    @Override
    public void buildOs() {
        apple.setOs("苹果系统");
    }

    @Override
    public void buildNetwork() {
        apple.setNetwork("双卡双待");
    }

    @Override
    public void buildBattery() {
        apple.setBattery("10000mAh");
    }

    @Override
    public Phone getPhone() {
        return apple;
    }
}

测试

package cn.sh.designepattern.example01;

/**
 * @Author song
 * @Version 0.0.1
 * @Date 2025/5/5 18:21
 * @Contact 643947568@qq.com
 */
public class Main {
    public static void main(String[] args) {
        PhoneBuilder androidBuilder = new AndroidBuilder();
        androidBuilder.buildBattery();
        androidBuilder.buildNetwork();
        androidBuilder.buildCpu();
        androidBuilder.buildOs();
        System.out.println(androidBuilder.getPhone());


        PhoneBuilder appleBuilder = new AppleBuilder();
        appleBuilder.buildBattery();
        appleBuilder.buildNetwork();
        appleBuilder.buildCpu();
        appleBuilder.buildOs();
        System.out.println(appleBuilder.getPhone());
    }
}

应用

可以再关注细节和流程的系统中使用,提取系统框架,封装构建过程等等。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不当菜虚困

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值