美文网首页
Builder建造者模式

Builder建造者模式

作者: simoscode | 来源:发表于2019-05-18 16:41 被阅读0次

    前言

    构建者模式,是用来构建我们的对象。如果我们构建的对象有很多属性值要设置,使用大量的setXX(),这样代码不利于阅读,也不整洁。

    实现

    我们假设有创建的是一个订单。使用构建者模式如下。PS:通常的构建者模式中是不提供setXX()方法,一旦对象构建以后就不允许修改,但是实际业务场景中有时候创建完一个对象,后续根据情形需要单独修改某个属性值,所以提供了setXX().

    /**
      * 订单
     */
    public class Order {
    /**
     * 价格
     */
    private Double price;
    /**
     * 订单号
     */
    private String orderId;
    /**
     * 寄件人姓名
     */
    private String sendMan;
    /**
     * 寄件人地址
     */
    private String sendAddress;
    /**
     * 寄件人电话
     */
    private String sendMobile;
    /**
     * 收件人
     */
    private String receiveMan;
    /**
     * 收件人地址
     */
    private String receiveAddress;
    /**
     * 收件人电话
     */
    private String receiveMobile;
    /**
     * 备注
     */
    private String remark;
    
    public Double getPrice() {
        return price;
    }
    
    public void setPrice(Double price) {
        this.price = price;
    }
    
    public String getOrderId() {
        return orderId;
    }
    
    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
    
    public String getSendMan() {
        return sendMan;
    }
    
    public void setSendMan(String sendMan) {
        this.sendMan = sendMan;
    }
    
    public String getSendAddress() {
        return sendAddress;
    }
    
    public void setSendAddress(String sendAddress) {
        this.sendAddress = sendAddress;
    }
    
    public String getSendMobile() {
        return sendMobile;
    }
    
    public void setSendMobile(String sendMobile) {
        this.sendMobile = sendMobile;
    }
    
    public String getReceiveMan() {
        return receiveMan;
    }
    
    public void setReceiveMan(String receiveMan) {
        this.receiveMan = receiveMan;
    }
    
    public String getReceiveAddress() {
        return receiveAddress;
    }
    
    public void setReceiveAddress(String receiveAddress) {
        this.receiveAddress = receiveAddress;
    }
    
    public String getReceiveMobile() {
        return receiveMobile;
    }
    
    public void setReceiveMobile(String receiveMobile) {
        this.receiveMobile = receiveMobile;
    }
    
    public String getRemark() {
        return remark;
    }
    
    public void setRemark(String remark) {
        this.remark = remark;
    }
    /**
     * 静态方法获取bulider
     * @return
     */
    public static Builder Builder(){
        return new Builder();
    }
      /**
     * Builder是public static 内部类.
     * 1 pulblic 为了扩大访问域,包外也可以
     * 2 static 是为了Order方法需要通过静态方法获取Builder
     */
    public static class Builder{
        private Double price;
        private String orderId;
        private String sendMan;
        private String sendAddress;
        private String sendMobile;
        private String receiveMan;
        private String receiveAddress;
        private String receiveMobile;
        private String remark;
        public Builder price(Double price){
            this.price = price;
            return this;
        }
        public Builder orderId(String orderId){
            this.orderId = orderId;
            return this;
        }
        public Builder sendMan(String sendMan){
            this.sendMan = sendMan;
            return this;
        }
        public Builder sendAddress(String sendAddress){
            this.sendAddress = sendAddress;
            return this;
        }
        public Builder sendMobile(String sendMobile){
            this.sendMobile = sendMobile;
            return this;
        }
        public Builder receiveMan(String receiveMan){
            this.receiveMan = receiveMan;
            return this;
        }
        public Builder receiveAddress(String receiveAddress){
            this.receiveAddress = receiveAddress;
            return this;
        }
    
        public Builder receiveMobile(String receiveMobile){
            this.receiveMobile = receiveMobile;
            return this;
        }
        public Builder remark(String remark){
            this.remark = remark;
            return this;
        }
        public Order build(){
            Order order = new Order();
            order.orderId = this.orderId;
            order.price = this.price;
            order.receiveAddress = this.receiveAddress;
            order.receiveMan = this.receiveMan;
            order.receiveMobile = this.receiveMobile;
            order.price = this.price;
            order.sendAddress = this.sendAddress;
            order.sendMan = this.sendMan;
            order.sendMobile = this.sendMobile;
            order.remark = this.remark;
            return order;
            
        }
    
    }
    }
    

    使用方式:

    public class DesignPatternApplication {
    public static void main(String[] args) {
        Order order = Order.Builder().orderId("123").price(12.4).receiveMan("dd")
                .receiveAddress("dd").sendAddress("dd").remark("ddada").build();
        //后续业务场景需要,可以修改某个属性
        order.setRemark("666");
        System.out.println(order.getOrderId());
    }
    }
    

    不重复造轮子,使用lombok

    复杂的系统中,为了开发人员更专注于业务,不必要编写AllArgsConstructor,NoArgsConstructor,Builder,getter,setter,建议使用lombok。通过简单的注解,即可完成以上重复繁琐的工作,引入lombok后编译器在编译的时候会生成包含以上内容class文件,感兴趣的小伙伴可以看看引入lombok后生成的class文件

    相关文章

      网友评论

          本文标题:Builder建造者模式

          本文链接:https://www.haomeiwen.com/subject/hpxamftx.html