Application of builder mode in Java

Source: Internet
Author: User

In the design mode, the builder mode is defined as a mode for building complex objects. The constructed objects often need to be initialized or assigned values in multiple steps. So, in the actual development process, where do we use the builder mode? It is a good practice to use the builder mode to replace the multi-parameter constructor.

We often have to write such an implementation class (assuming the class name is dodocontact), which has multiple constructors,

Dodocontact (string name );

Dodocontact (string name, int age );

Dodocontact (string name, int age, string address );

Dodocontact (string name, int age, string address, int cardid );

The main purpose of such a series of constructor functions is to provide more customer call options to process different constructor requests. This method is common and effective, but it has many disadvantages. The author of the class has to write constructor with multiple parameter combinations, and also needs to set the default parameter value. This is a careful and boring job. Second, this constructor is not flexible, and you have to provide meaningless parameter values during calling, such as dodocontact ("Ace",-1, "sh"). Obviously, it doesn't make sense to have a negative age, but you don't need to do this, so it can comply with Java specifications. IfCodeAfter the release, the maintainer will have a headache because he does not know what this-1 means. In this case, the builder mode is very suitable. The main point of the builder mode is to build an object through a proxy. This agent is responsible for completing the construction steps and is also easy to expand. The following is a piece of code written from objective Java:

 public class dododocontact {private final int age; private final int safeid; private final string name; private final string address; Public int getage () {return age;} public int getsafeid () {return safeid;} Public String getname () {return name;} Public String getaddress () {return address ;} public static class builder {private int age = 0; private int safeid = 0; private string name = NULL; private string address = NULL; 
// Construct the public Builder (string name) {This. name = Name;} public builder age (INT Val) {age = val; return this;} public builder safeid (INT Val) {safeid = val; return this ;} public builder address (string Val) {address = val; return this;} public dodocontact build () {// build, return a new object return New dodocontact (this );}} private dodocontact (builder B) {age = B. age; safeid = B. safeid; name = B. name; address = B. address ;}}

End, customerProgramThis object can be built flexibly.

 
Dodocontact DDC = new dodocontact. builder ("Ace "). age (10 ). address ("Beijing "). build (); system. out. println ("name =" + DDC. getname () + "age =" + DDC. getage () + "Address" + DDC. getaddress ());

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.