Builder model of JAVA learning __java

Source: Internet
Author: User
Tags static class

Builder mode principle, building the target class through static inner classes
Benefits:
1. What attributes are required to fill in the property, do not need to build different constructors for different instances
2. Chained writing

Package com.free.framwork.jdk8.builder;

Import Java.util.Date;  /** * Com.free.framwork.jdk8.builder.Student * Builder mode * @author Lipeng * @dateTime 2017/8/26 12:17/public class

    Student {private Integer ID;

    private String name;

    Private Integer age;

    Private Date birthday;
        Private Student (Builder Builder) {this.id = builder.id;
        THIS.name = Builder.name;
        This.age = Builder.age;
    This.birthday = Builder.birthday; @Override public String toString () {return "student{" + "id=" + ID + " 
    , name= ' "+ name + ' \ ' +", age= "+ Age +", birthday= "+ Birthday + '}";

        public static class builder{private Integer ID;

        private String name;

        Private Integer age;

        Private Date birthday;
        Build Builder Object public static Builder Builder () {return new Builder (); }

        Public Builder ID (Integer id) {this.id = ID;
        return this;
            Public Builder name (String name) {this.name = name;
        return this;
            Public Builder Age (Integer age) {this.age = age;
        return this;
            Public Builder birthday (Date birthday) {this.birthday = birthday;
        return this;
        ///through the Builder object to build the Student object public Student builds () {return to New Student (this); }
    }
}
package com.free.framwork.jdk8.builder;

Import Java.util.Date; /** * Com.free.framwork.jdk8.builder.TestBuilder * * * @author Lipeng * @dateTime 2017/8/26 12:25/public class Testb
                Uilder {public static void main (string[] args) {Student Student = Student.Builder.builder () . ID (10001). Name ("Test"). Age. Birthday (new Date ()). Bu

        ILD (); Student student1 = Student.Builder.builder (). ID (10002). Name ("Test 1"). Age (

        Birthday (New Date ()). build ();
        SYSTEM.OUT.PRINTLN (student);
        System.out.println ("======================");
    System.out.println (STUDENT1); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.