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); }
}