Go The application of builder mode in Java

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/moonz-wu/archive/2011/01/11/1932473.html

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:

public class Dodocontact {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 = 0;        private int safeid = 0;        Private String name = NULL;        Private String address = null;        Steps to build 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; }}

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

Dodocontact DDC = new Dodocontact.builder ("Ace"). Age (Ten).                address ("Beijing"). Build (); System.out.println ("name=" + ddc.getname () + "age =" + ddc.getage ()                + "address" + ddc.getaddress ());

  

Go 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.