JAVA建造者模式

看到别人的代码顿觉精妙,记录留存。

应用场景:假设有一个问题,我们需要创建一个学生对象,属性有name,number,class,sex,age,school等属性,如果每一个属性都可以为空,也就是说我们可以只用一个name,也可以用一个school,name,或者一个class,number,或者其他任意的赋值来创建一个学生对象,这时该怎么构造?

难道我们写6个1个输入的构造函数,15个2个输入的构造函数.......吗?这个时候就需要用到Builder模式了。

public class Builder {

    static class Student{
        String name = null ;
        int number = -1 ;
        String sex = null ;
        int age = -1 ;
        String school = null ;
        static class StudentBuilder{
            String name = null ;
            int number = -1 ;
            String sex = null ;
            int age = -1 ;
            String school = null ;
            public StudentBuilder setName(String name) {
                this.name = name;
                return  this ;
            }

            public StudentBuilder setNumber(int number) {
                this.number = number;
                return  this ;
            }

            public StudentBuilder setSex(String sex) {
                this.sex = sex;
                return  this ;
            }

            public StudentBuilder setAge(int age) {
                this.age = age;
                return  this ;
            }

            public StudentBuilder setSchool(String school) {
                this.school = school;
                return  this ;
            }
            public Student build() {
                return new Student(this);
            }
        }

        public Student(StudentBuilder builder){
            this.age = builder.age;
            this.name = builder.name;
            this.number = builder.number;
            this.school = builder.school ;
            this.sex = builder.sex ;
        }
    }

    public static void main( String[] args ){
        /**
         * Student.StudentBuilder()方法新建StudentBuilder静态类,然后调用setAge方法设置年龄,由于所有
         * set方法都返回StudentBuilder类对象,所以可以一直调用,直至需要设置的属性设置完毕然后调用Student
         * 有参构造方法,以StudentBuilder为参数,将刚才所设置的参数一一赋值,关键在于每个set函数返回值都是
         * 一个StudentBuilder对象,这样才能灵活调用所有设置方法
         */

        Student a = new Student.StudentBuilder().setAge(13).setName("LiHua").build();
        Student b = new Student.StudentBuilder().setSchool("sc").setSex("Male").setName("ZhangSan").build();
    }
}

研究清楚了这个方法扩展思考:干嘛非得多建静态类,于是有了下面代码:

class Human {
    private String name;
    private int age;
    private String sex;

    public Human setName(String name) {
        this.name = name;
        return this;
    }
    public Human setAge(int age) {
        this.age = age;
        return this;
    }
    public Human setSex(String sex) {
        this.sex = sex;
        return this;
    }
    
     public Human Human(){
        return this;
     }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getSex() {
        return sex;
    }
}

public class Builder {

    static class Student{
        String name = null ;
        int number = -1 ;
        String sex = null ;
        int age = -1 ;
        String school = null ;
        static class StudentBuilder{
            String name = null ;
            int number = -1 ;
            String sex = null ;
            int age = -1 ;
            String school = null ;
            public StudentBuilder setName(String name) {
                this.name = name;
                return  this ;
            }

            public StudentBuilder setNumber(int number) {
                this.number = number;
                return  this ;
            }

            public StudentBuilder setSex(String sex) {
                this.sex = sex;
                return  this ;
            }

            public StudentBuilder setAge(int age) {
                this.age = age;
                return  this ;
            }

            public StudentBuilder setSchool(String school) {
                this.school = school;
                return  this ;
            }
            public Student build() {
                return new Student(this);
            }
        }

        public Student(StudentBuilder builder){
            this.age = builder.age;
            this.name = builder.name;
            this.number = builder.number;
            this.school = builder.school ;
            this.sex = builder.sex ;
        }
    }

    public static void main( String[] args ){
        /**
         * Student.StudentBuilder()方法新建StudentBuilder静态类,然后调用setAge方法设置年龄,由于所有
         * set方法都返回StudentBuilder类对象,所以可以一直调用,直至需要设置的属性设置完毕然后调用Student
         * 有参构造方法,以StudentBuilder为参数,将刚才所设置的参数一一赋值,关键在于每个set函数返回值都是
         * 一个StudentBuilder对象,这样才能灵活调用所有设置方法
         */

        Student a = new Student.StudentBuilder().setAge(13).setName("LiHua").build();
        Student b = new Student.StudentBuilder().setSchool("sc").setSex("Male").setName("ZhangSan").build();
        Human h = new Human().setAge(1).setName("张三");
    }
}

也能实现此功,并且看上去更简单点,或许原代码有其他拓展功能,各位求指教。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值