Implementation of Builder mode

Source: Internet
Author: User

The builder mode is useful for situations where multiple parameters are required when constructing an object. For multiple parameters, the method name can also be used to illustrate the purpose.

But in order to construct an object, it is necessary to create its builder Builder, which can be a performance issue in some very performance-oriented situations.

Implementation Mode 1:

public class book {private final string author;    // Must parameter private final string name;//must parameter private final string isbn;//must parameter private  final string language;        //non-mandatory parameter private  final int weight;//non-mandatory parameter Private book (Builder builder) {author = builder.author; Name = builder.name;isbn = builder. Isbn;language = builder.language;weight = builder.weight;} public static class builder{private final string author;private final  String name;private final string isbn;private string language;private int  weight;public builder (STRING AUTHOR, STRING NAME, STRING ISBN)  { THIS.AUTHOR = AUTHOR;THIS.NAME = NAME;ISBN = ISBN;} Public builder language (string&Nbsp;language) {this.language = language;return this;} Public builder weight (int weight) {this.weight = weight;return this;} Public book build () {return null;}} @Overridepublic  string tostring ()  {return  "book [author="  + author +   ",  name="  + name +  ",  isbn="  + ISBN+  ",  language="  + language +  ",  weight="  + weight +  "]";}}

How to use:

Book book = new Book.builder ("Author", "Name", "ISBN"). Language ("language"). Weight (2). build ();


Implementation Mode 2:

public class book {private final string author;    // Must private final string name;//must private final string isbn;//must be private final  string language;    //must private final int weight;//non-mandatory private  book (builder builder) {author = builder.author;name = builder.name;isbn =  builder. Isbn;language = builder.language;weight = builder.weight;} Public static builder builder (STRING AUTHOR,STRING NAME,STRING ISBN) {return  new builder (AUTHOR,NAME,ISBN);} Private static class builder{private final string author;private final  string name;private final string isbn;private string language;private  int weight;public builder (STRING AUTHOR, STRING NAME, STRING ISBN)  {this.autHOR = AUTHOR;THIS.NAME = NAME;ISBN = ISBN;} Public builder language (string language) {this.language = language;return this;} Public builder weight (int weight) {this.weight = weight;return this;} Public book build () {return null;}} @Overridepublic  string tostring ()  {return  "book [author="  + author +   ",  name="  + name +  ",  isbn="  + ISBN+  ",  language="  + language +  ",  weight="  + weight +  "]";}}


How to use:

Book book = Book.builder ("Author", "Name", "ISBN"). Language ("language"). Weight (2). build ();


Implementation of Builder mode

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.