Java设计模式Builder建造者模式,Builder设计模式简单代码示例

示例代码应用场景
在创建对象然后进行赋值的时,如果参数过多可能会造成部分重要参数没有set,用Builder进行赋值可以对必须值进行判断防止出现重要参数遗漏检测,例如下面示例代码中我调用build时我对价格是否为null进行了判定

public class CommodityInstance {
    private Long consulTaskId; // 任务外键

    private Long productId;// 产品外键 ,这个商品是通过哪个产品实例化的

    private Long picId; // 这个SKU属性值所对应的图片ID

    private String propValue; // sku属性值

    private Long quantity;// 库存

    private String outerId;// 商品编码

    private Double price;// 价格

    public CommodityInstance(Builder builder) {
        this.consulTaskId = builder.consulTaskId;
        this.productId = builder.productId;
        this.propValue = builder.propValue;
        this.quantity = builder.quantity;
        this.outerId = builder.outerId;
        this.price = builder.price;
        this.picId = builder.picId;
    }


    public static class Builder {
        private Long consulTaskId; // 任务外键

        private Long productId;// 产品外键 ,这个商品是通过哪个产品实例化的

        private String propValue; // sku属性值

        private Long picId; // 这个SKU属性值所对应的图片ID

        private Long quantity;// 库存

        private String outerId;// 商品编码

        private Double price;// 价格

        public Builder consulTaskId(Long consulTaskId) {
            this.consulTaskId = consulTaskId;
            return this;
        }

        public Builder picId(Long picId) {
            this.picId = picId;
            return this;
        }

        public Builder productId(Long productId) {
            this.productId = productId;
            return this;
        }

        public Builder propValue(String propValue) {
            this.propValue = propValue;
            return this;
        }

        public Builder quantity(Long quantity) {
            this.quantity = quantity;
            return this;
        }

        public Builder outerId(String outerId) {
            this.outerId = outerId;
            return this;
        }

        public Builder price(Double price) {
            this.price = price;
            return this;
        }

        public CommodityInstance build() {
            if (this.price == null) throw new NullPointerException("商品价格不能为空");
            return new CommodityInstance(this);
        }
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CommodityInstance that = (CommodityInstance) o;
        return propValue != null ? propValue.equals(that.propValue) : that.propValue == null;
    }

    @Override
    public int hashCode() {
        int result = 31 + (propValue != null ? propValue.hashCode() : 0);
        return result;
    }
}

使用代码示例

 CommodityInstance commodity = new CommodityInstance.Builder()
                    .consulTaskId(consulTask.getId())
                    .price(x.getPrice())
                    .picId(x.getId())
                    .outerId(x.getOuterId())
                    .propValue(x.getPropValue())
                    .productId(x.getProductId())
                    .quantity(x.getQuantity()).build();

本次只是进行简单的演示,在Builder建造者模式中每个方法还可以进行更多有趣操作,可以将更多业务代码隐藏起来,例如下图
在这里插入图片描述
在这里插入图片描述
这样使用就可以隐藏很多业务实现的细节,调用者只需要关心最终为他构建的什么样的对象数据就可以了

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Acmen-zym

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

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

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

打赏作者

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

抵扣说明:

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

余额充值