whats the number of times the letter s appears in:
q: why is the builder-pattern often implemented like this? often i see the implementation of the builder pattern (in java) to be like this:
public class foo {
    private foo(foobuilder builder) {
        // get alle the parameters from the builder and apply them to this instance
    }

    public static class foobuilder {
        // ...
        public foo build() {
            return new foo(this); // <- this part is what irritates me
        }
    }
}

examples also here:


*

*https://stackoverflow.com/questions/25130510/builder-pattern-in-java

*https://www.javacodegeeks.com/2013/01/the-builder-pattern-in-practice.html
why do people introduce the strong (mutual) dependency between builder and type to build?
edit: i mean, i know that by its nature the builder is tied to the class i want to build an instance from (foobuilder -> foo). what i questioning is: why is there the necessity for the other way around (foo -> foobuilder)
on the contrary i see (less often) implementations that create a new instace of foo when the builder is created and set the fields on the instance of foo when the appropriate builder-methods are called. i like this approach better, because it still has a fluent api and does not tie the class to its builder. is there any downside of this other approach?

a: it boils down to never having a foo in a half constructed state
some reasons that spring to mind:
the builders is by nature tied to the class it is constructing. you can think of the intermediate states of the builder as partial application of the constructor, so your argument that it is too coupled isnt  persuasive.
by passing the whole builder in, fields in myclass can be final, making reasoning about myclass easier. if the fields are not final, you could just as easily have fluent setters than a builder.

a: the builder will hold mutable fields, set with methods. the actual class (foo) can then create immutable final fields from the builder.
so the builder is a stateful class.
but the builder could alse have called new foo(x, y, z). that would be more one-directional, better style imho. as then the anti-pattern, a foobuilder field or such would not be possible. on the other hand the foobuilder guarantees some integrity of the data.
on the other hand foobuilder plays two roles: for building with a fluent api, and for presenting data to the constructor. it is an event-type class for the constructor. in a overdesigned code the constructor params could be kept in some fooconstructorparams.

a: because a builder is typically used to solve the telescoping constructor problem, especially in context of immutable classes. see this link for a full-fledged example.
consider what the alternatives would look like:
 public static class foobuilder {
    // works with immutable classes, but leads to telescoping constructor
    public foo build() {
        return new foo(par1, par2, par3, ....); 
    }
 }

or:
 public static class foobuilder {
    // does not work for immutable classes:
    public foo build() {
        var foo = new foo(); 
        foo.attribute1=par1;
        foo.attribute2=par2;
        //...
        return foo;
    }
 }

so especially for immutable classes, if you want no constructor with a bunch of parameters, there is actually no alternative.

user
There are one hundred and sixty-five "s"s in the text you provided.
assistant