Create an object's builder pattern __java

Source: Internet
Author: User
* * Create an object's builder mode * Builder mode is only suitable for scenarios where you need to specify multiple optional parameters when creating an object.
 * Client: * 1: In the client can obtain the person object through the following code * Person.personbuilder builder=new Person.personbuilder ("111", "Wanda Less", "male");
 * Here to select the optional parameter age hobby for example demo; * Person person=builder.setage. Sethobby ("reading")-build ();
    * * public class Person {* * * necessary parameters * ID number, name, sex * Declaration as private to avoid external unsafe modification/private String ID;
    private String name;
    Private String sex;
    * * Optional Parameters * age, telephone, hobby, address, educational background;/private int ages;
    Private String phone;
    Private String hobby;
    Private String addr;
    Private String major;
     * * * The Person class private constructor, why use a private constructor.
    	* * Private person (Personbuilder builder) {this.id=builder.id;
    	This.name=builder.name;
    	This.sex=builder.sex;
    	This.age=builder.age;
    	This.phone=builder.phone;
    	This.hobby=builder.hobby;
    	THIS.ADDR=BUILDER.ADDR;
    This.major=builder.major; * * * Call Setter,getter method to get change Private member */public void setId (String ID) {this.id=id;
	Public String GetId () {return id; * * * This personbuilder is specifically used to create a person object, * So the Personbuilder class is subordinate to the person class. Therefore, the use of the * inner class can clearly express this subordination. Use the "Static" * inner class to create a Personbuilder object when no person object exists/public static class Personbuilde
        r{* * Necessary parameters * ID number, name, gender * * Private String ID;
        private String name;
        Private String sex;
        * * Optional Parameters * age, telephone, hobby, address, educational background;/private int ages;
        Private String phone;
        Private String hobby;
        Private String addr;
        Private String major; * * To have the client invoke the constructor using all the necessary parameters to get a Builder object/public Personbuilder (String id,string name,string Sex
        	) {this.id=id;
        	This.name=name;
        This.sex=sex;
        * * * Call the parameterless build method to create the Person object * Note that the person object is not created before the build method call, so there is no need to worry about the issue of early disclosure.
  Public person build () {      	Return to new person (this); 
         * * * Invokes a setter-like method to set each of the relevant optional parameters * Here is the Personbuilder object, which makes it easy to set optional parameters in cascading mode, so that the hierarchy is clear and the code is simple
			* * Public personbuilder setage (int age) {this.age = age;
		return this;
			Public Personbuilder Setphone (String phone) {this.phone = phone;
		return this;
			Public Personbuilder sethobby (String hobby) {this.hobby = hobby;
		return this;
			Public Personbuilder setaddr (String addr) {this.addr = addr;
		return this;
			Public Personbuilder Setmajor (String major) {this.major = major;
		return this;
 }
        
    }
}

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.