The application of builder mode in Java

Source: Internet
Author: User

The builder pattern is defined in design mode as a pattern for building complex objects that often require multiple-step initialization or assignment to complete. So, in the actual development process, where do we fit the builder model? It is a good practice to use builder mode instead of multi-parameter constructors.

We often face the writing of such an implementation class (assuming that the class is called 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 constructors is to provide more customer invocation options to handle different construction requests. This approach is common and effective, but it has many drawbacks. The author of the class has to write a constructor for various combinations of parameters, and it also needs to set the default parameter values, which is an attentive and tedious task. Second, the flexibility of such a constructor is not high, and you have to provide some meaningless parameter values at the time of invocation, for example, Dodocontact ("Ace",-1, "SH"), obviously the age of negative is meaningless, but you do not do so, to conform to Java specifications. If such code is released, the maintainer behind it will have a headache because he doesn't know what the 1 means. For such cases, it is very well suited to use the builder mode. The key point of the builder mode is to complete the object construction process through an agent. This is the agent's responsibility to complete the steps of the build, and it is also easy to expand. Here's a piece of code that's rewritten from effective Java:

123456789101112131415161718192021222324252627 publicclassDoDoContact {    privatefinalint age;    privatefinalintsafeID;    privatefinalString name;    privatefinalString address;     publicintgetAge() {        returnage;    }    publicintgetSafeID() {        returnsafeID;    }    publicString getName() {        returnname;    }    public String getAddress() {        returnaddress;    }    publicstaticclassBuilder {        privateint age     = 0;        privateintsafeID  = 0;        privateString name    = null;        privateString address = null;
123456789101112131415161718192021222324252627282930313233    // 构建的步骤        publicBuilder(String name) {            this.name = name;        }        publicBuilder age(intval) {            age = val;            returnthis;        }        publicBuilder safeID(intval) {            safeID = val;            returnthis;        }        publicBuilder address(String val) {            address = val;            returnthis;        }        publicDoDoContact build() { // 构建,返回一个新对象            returnnewDoDoContact(this);        }    }    privateDoDoContact(Builder b) {        age = b.age;        safeID = b.safeID;        name = b.name;        address = b.address;    }}

Finally, the client program can be very flexible to build this object.

1234 DoDoContact ddc = newDoDoContact.Builder("Ace").age(10)                .address("beijing").build();System.out.println("name=" + ddc.getName() + "age ="+ ddc.getAge()                + "address"+ ddc.getAddress());

The application of builder mode in Java

Related Article

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.