Builder pattern

Share

Consider using a builder pattern when faced with classes that have constructors that take a large number of parameters.
One way to handle many parameters when instantiating a class is by using the pattern known as the telescoping constructor pattern in which we provide a constructor that takes only the required parameters, another one that takes an additional extra parameter, another constructor with two more extra parameters, another one with three and so on.
For example we have the class:

public class SkittlesBag{
 
    private final int red; // required
    private final int blue; // required
    private final int yellow; // optional
    private final int green; // optional
    private final int purple; // optional
    private final int pink; // optional
 
    public SkittlesBag(int red, int blue) {
        this(red, blue, 0);
    }
    public SkittlesBag(int red, int blue, int yellow) {
        this(red, blue, yellow, 0);
    }
    public SkittlesBag(int red, int blue, int yellow, int green) {
        this(red, blue, yellow, green, 0);
    }
    public SkittlesBag(int red, int blue, int yellow, int green, int purple) {
        this(red, blue, yellow, green, purple, 0);
    }
    public SkittlesBag(int red, int blue, int yellow, int green, int purple, int pink) {
        this.red= red;
        this.blue= blue;
        this.yellow= yellow;
        this.green= green;
        this.purple= purple;
        this.pink= pink;
    }
}

It is quite hard to write client code and also hard to read at object creation. There are optional parameters that have to be set even if they are not of interest.
When you want to create an instance, you use the constructor with the shortest
parameter list containing all the parameters you want to set:

SkittlesBag bag = new SkittlesBag(123, 8, 84, 0, 44, 45);

This might not look so complicated when you have only six parameters to set, but it will soon get messy if there are going to be more. Also, if there are many parameters of the same type, it is easy to switch their positions by mistake when instantiating the object, and the compiler won’t complain, but we end up with objects that do not meet our first intention.

As a better alternative, the builder pattern works like this:

Instead of making the desired object directly, the client calls a constructor with all of the required parameters and gets a builder object. Then the client calls setter-like methods on the builder object to set each optional parameter of interest. Finally, the client calls a parameter less build method to generate the object, which is immutable. The builder is a static member class of the class it builds.

// Builder Pattern
public class SkittlesBag{
 
    private final int red;
    private final int blue;
    private final int yellow;
    private final int green;
    private final int purple;
    private final int pink;
 
    public static class Builder {
 
        // Required parameters
        private final int red;
        private final int blue;
        // Optional parameters - initialized to default values
        private int yellow = 0;
        private int green = 0;
        private int purple = 0;
        private int pink = 0;
 
        public Builder(int red, int blue) {
            this.red = red;
            this.blue = blue;
        }
        public Builder yellow(int val)
        { yellow = val; return this; }
 
        public Builder green(int val)
        { green = val; return this; }
 
        public Builder purple(int val)
        { purple = val; return this; }
 
        public Builder pink(int val)
        { pink = val; return this; }
 
        public SkittlesBag build() {
            return new SkittlesBag(this);
        }
    }
 
    private SkittlesBag(Builder builder) {
        red = builder.red;
        blue = builder.blue;
        yellow = builder.yellow;
        green = builder.green;
        purple = builder.purple;
        pink = builder.pink;
    }
}

Note that SkittlesBag is immutable, and that all parameter default values
are in a single location. The builder’s setter methods return the builder itself so
that invocations can be chained.

SkittlesBag bag = new SkittlesBag.Builder(240, 8).yellow(100).green(35).pink(27).build();

This client code is easy to write and, more importantly, to read.

Source: Effective Java – second edition by Joshua Block

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close