Builder模式处理多个构造器

Builder模式拥有重叠构造起器的安全性和JavaBeans模式的可读性。的确,看起来代码舒服好多。当构造器参数比较多的时候推荐使用,构造器比较少的时候就不需要使用这个了。之前封装OKhttp的时候,发现Request这个类就是使用这个模式,可以借鉴一下。

这个模式其实就是构建Builder静态内部类,在Apple类的构造方法中初始化Apple对象。代码如下:
public class Apple {
    private final int a;
    private final int b;
    private final int c;
    private final int d;
    private final int e;

    public static class Builder {
        private final int a;
        private final int b;
        private int c = 0;
        private int d = 0;
        private int e = 0;

        public Builder(int a, int b) {
            this.a = a;
            this.b = b;
        }

        public Builder setC(int c) {
            this.c = c;
            return this;
        }

        public Builder setD(int d) {
            this.d = d;
            return this;
        }

        public Builder setE(int e) {
            this.e = e;
            return this;
        }

        public Apple build() {
            return new Apple(this);
        }
    }

    private Apple(Builder builder) {
        this.a = builder.a;
        this.b = builder.b;
        this.c = builder.c;
        this.d = builder.d;
        this.e = builder.e;
    }

    public static void main(String[] args) {
        Apple apple = new Apple.Builder(1, 1).setC(12).setD(14).build();//初始化对象
    }


}
对比一下重叠构造器:
public class MyLinearLayout {
private int a;
private int b;
private int c;
private int d;
private int e;

public MyLinearLayout() {
this(0, 0);
    }

public MyLinearLayout(int a, int b) {
this(a, b, 0);
    }

public MyLinearLayout(int a, int b, int c) {
this(a, b, c, 0);
    }

public MyLinearLayout(int a, int b, int c, int d) {
this(a, b, c, d, 0);
    }

public MyLinearLayout(int a, int b, int c, int d, int e) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
    }
}

有木有感觉可读性高了好多。

### Java Builder 设计模式概述 Builder设计模式属于创建型模式之一,在Java编程中广泛应用。这种模式允许分步骤地构造复杂对象,使得同样的构建过程能够创建不同的表示[^1]。 ### 原理说明 当一个类具有多个属性,并且这些属性可能不是全部都需要初始化时,使用传统的构造函数方式可能会变得非常繁琐。此时采用Builder模式可以简化对象的创建流程。通过引入内部静态类`Builder`来逐步设置各个字段,最后调用`build()`方法返回最终的对象实例。 ### 使用示例 下面是一个简单的例子展示如何利用Builder模式创建复杂的对象: #### 定义带有Builder模式的类 ```java public class Phone { private final String brand; private final int ram; private final double screenSize; // 私有化构造器防止外部直接new private Phone(Builder builder){ this.brand = builder.brand; this.ram = builder.ram; this.screenSize = builder.screenSize; } @Override public String toString(){ return "Brand:" + brand + ", RAM:" + ram +"GB, Screen Size:"+screenSize+" inches"; } // 静态内部类作为Builder public static class Builder{ private String brand; private int ram; private double screenSize; public Builder setBrand(String brand){ this.brand = brand; return this; // 返回当前Builder实例以便链式调用 } public Builder setRam(int ram){ this.ram = ram; return this; } public Builder setScreenSize(double screenSize){ this.screenSize = screenSize; return this; } // 构建并返回完整的Phone对象 public Phone build(){ return new Phone(this); } } } ``` #### 创建对象实例 ```java // 使用Builder模式创建Phone对象 Phone myPhone = new Phone.Builder() .setBrand("Apple") .setRam(8) .setScreenSize(6.1) .build(); System.out.println(myPhone); // 输出: Brand:Apple, RAM:8GB, Screen Size:6.1 inches ``` 上述代码展示了如何定义一个支持Builder模式的`Phone`类以及怎样使用这个模式去创建具体的手机对象。值得注意的是,这里重写了`toString()`方法,因此可以直接打印出有意义的信息而不是默认的对象地址[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值