Effective Java Reading Notes value 2nd: Consider using a constructor when you encounter multiple constructor parameters

Source: Internet
Author: User

For a class with multiple constructors for a parameter, programmers have always used overlapping constructor patterns.

public class Person {private string id;private string Name;private string address;public person (string id) {id = ID;} Public person (string name, string address) {super (); this.name = name;this.address = Address;} Public person (string ID, string name, string address) {id = Id;this.name = name;this.address = Address;}}

The overlapping constructor pattern is feasible, but when there are many parameters, the client code is difficult to write. and difficult to read. When there are many constructor parameters, there is a second alternative, the JavaBean pattern (which can now be said to be very popular). In this mode, a parameterless constructor is called to create the object, and then the setter method is called to set each necessary parameter. And each of the relevant optional parameters:

public class Person {private string id;private string Name;private string address;public person () {super ();} Public String GetId () {return Id;} public void SetId (String ID) {id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String getaddress () {return address;} public void setaddress (String address) {this.address = address;}}

In this case, each time we use that parameterless constructor, and then we want to set which variable to set which. But the JavaBeans model itself has a serious disadvantage. Because the construction process is divided into several invocations. JavaBean may be in an inconsistent state during the construction process. Classes cannot guarantee consistency simply by verifying the validity of constructor parameters.


Fortunately, there is a third alternative, which guarantees security like the overlapping constructor pattern, as well as the readability of the JavaBeans pattern. This is the builder mode. Instead of generating the desired object directly, let the client call the constructor with all the necessary parameters to get a builder object. The client then uses a setter-like method to set each relevant optional parameter on the Builder object. Finally, the client calls the parameterless builder method to generate the immutable object. This builder is the static member class of the class of its builder.

public class Person {private string id;private string Name;private string address;public the parameters necessary for static class builder{//Priv Ate final string id;//optional parameter private String name = "";p rivate string address = "";p ublic Builder (String Id) {this. id = ID;} Public Builder name (String name) {this.name = Name;return this;} Public Builder address (String address) {this.address = Address;return this;} Public person Build () {return new person (this);}} private person (builder builder) {this. Id = Builder. Id;this.name = Builder.name;this.address = builder.address;}}

Call:

Person person = new Person.builder ("+"). Address ("SS"). Name ("SS"). Build ();

In short, if you have multiple parameters in a class's constructor or static factory, the builder mode is a good choice when designing this class, especially when most parameters are optional, and client-side code that uses the builder pattern is easier to read and write than using the traditional overlay constructor pattern , the constructor is also more secure than JavaBeans.


Effective Java Reading notes value 2nd: Consider using a constructor when you encounter multiple constructor parameters

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.