builder模式

在日常的开发过程中,有的时候会遇到new一个很多构造函数的对象,此时最简单的方式就是通过对象的构造函数传参去构造,另外一种就是可以通过builder模式去构建,两种方式分别见如下代码。

传统构造函数方式构建对象以及实例化:

    private String name;
    private String height;
    private int age;
    private String school;
    private String hobby;
    
    public People(String name,String height,int age,String school,String hobby){
        this.name = name;
        this.height = height;
        this.age = age;
        this.school = school;
        this.hobby = hobby;
    }
People people = new People("wang", "190", 45, "qingxiao", "sports");

builder模式构建多参数对象以及实例化:

    private String name;
    private String height;
    private int age;
    private String school;
    private String hobby;
    
    private People(Builder builder){
        this.name = builder.getName();
        this.height = builder.getHeight();
        this.age = builder.getAge();
        this.school = builder.getSchool();
        this.hobby = builder.getHobby();
    }
    
    public static class Builder{
        private String name;
        private String height;
        private int age;
        private String school;
        private String hobby;
        
        public Builder(){
        }
        
        public String getName() {
            return name;
        }
        public Builder name(String name) {
            this.name = name;
            return this;
        }
        public String getHeight() {
            return height;
        }
        public Builder height(String height) {
            this.height = height;
            return this;
        }
        public int getAge() {
            return age;
        }
        public Builder age(int age) {
            this.age = age;
            return this;
        }
        public String getSchool() {
            return school;
        }
        public Builder school(String school) {
            this.school = school;
            return this;
        }
        public String getHobby() {
            return hobby;
        }
        public Builder hobby(String hobby) {
            this.hobby = hobby;
            return this;
        }
        
        public People build(){
            return new People(this);
        }
    }

People people = new People.Builder().name("wang").height("190").age(45).school("qingxiao").hobby("sports").build();

看到以上这两种实例化对象的方式,相对传统的方式来说,不管是代码的可读性还是代码的维护性都是builder模式更加占优,因为在使用传统的方式的时候,是绝对不能弄乱传递参数的顺序的,而且也更容易弄乱,在有的情况下可能出现的参数个数是上面的参数个数的数倍,那就更加容易出错,而且在维护起来也不方便,乍看上去都不知道某个参数是什么含义;而使用builder模式看起来就简单多了,望文知意,而且如果有的参数不需要的话,也就不用设置,默认是空就行了。

builder模式有个不好的地方就是可能出现代码量增加的情况,但是相对于可读性和易用性以及后期的可维护性,这点缺点是微不足道的,因为增加的代码量完全可以通过开发工具快捷方式快速生成。

总之,在遇到有大量参数的对象并且其中参数可以为空的情况下,强烈推荐使用builder模式,确实足够强大。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值