Design mode: Construction mode

Source: Internet
Author: User

Original address: http://leihuang.org/2014/12/03/builder/

Creational mode

The production of objects needs to consume system resources, so how to produce, manage and manipulate objects efficiently is always a subject to be discussed, the creational model is related to the establishment of objects, and the model under this classification gives some guiding principles and the direction of design. Listed below are all belong to the creational mode

    • Simple Factory Mode
    • Abstract Factory Mode
    • Builder mode
    • Factory method Mode
    • Prototype mode
    • Singleton mode
    • Registry of Singleton Mode
Problems that arise

The builder pattern is a good choice when designing classes whose constructors or static factories would has more than a Handful of parameters.

When the parameters of the constructor are very long and there are multiple constructors, the situation is as follows:

Pizza (int size) {...} Pizza (int size, Boolean cheese) {...} Pizza (int size, Boolean cheese, Boolean pepperoni) {...} Pizza (int size, Boolean cheese, Boolean pepperoni, Boolean bacon) {...}

This is called the telescoping Constructor Pattern. Here's another workaround called JavaBean mode:

Pizza Pizza = new Pizza (n);p Izza.setcheese (True);p Izza.setpepperoni (True);p Izza.setbacon (true);

Javabeans make a class mutable even if it's not strictly necessary. So JavaBeans require a extra effort in handling Thread-safety (an immutable class are always thread safety!). See: JavaBean Pattern

Build mode to solve the problem
<span style= "Font-weight:normal;"    >public class Pizza {//mandatory private final int size;    Optional private Boolean cheese;    Private Boolean pepperoni;    private Boolean bacon;        public static class Builder {//required private final int size;        Optional private Boolean cheese = false;        Private Boolean pepperoni = false;        Private Boolean bacon = false;        Because size is required, the parameter set to the constructor is public Builder (int size) {this.size = size;            Public Builder Cheese (boolean value) {cheese = value;        return this;            Public Builder Pepperoni (boolean value) {pepperoni = value;        return this;            Public Builder Bacon (boolean value) {bacon = value;        return this;        Public Pizza Build () {Return to New Pizza (this);        }} private Pizza (Builder builder) {size = Builder.size; Cheese = Builder.cheese;        Pepperoni = Builder.pepperoni;    Bacon = Builder.bacon; }}</span>

Note that pizza is now a immutable class, so it is thread-safe. And because each builder's setter method returns a builder object, it can be called in series. As follows

Pizza Pizza = new Pizza.builder (n). Cheese (true). Pepperoni (true).                       build ();

By the above we can know that the construction mode, gives the user greater freedom, can be optional parameters.

2014-11-09 18:21:17

Brave,happy,thanksgiving!


Design mode: Construction 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.