Note that there are some explanatory texts on larger screens.

plurals
  1. POBuilder Pattern, with a public Constructor vaild
    primarykey
    data
    text
    <p>I know there are several questions about the builder pattern. <br> - <a href="https://stackoverflow.com/questions/10092458/use-builder-pattern-from-the-constructor-in-a-subclass">Use builder pattern from the constructor in a subclass</a> <br> - <a href="https://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern">When would you use the builder pattern</a> <br> - <a href="https://stackoverflow.com/questions/17380445/java-improving-builder-pattern-on-a-specific-class">Java improving builder pattern on a specific class</a> <br> - <a href="https://stackoverflow.com/questions/4313172/builder-design-pattern-why-do-we-need-a-director">Builder design pattern why do we need a director</a></p> <p>So far I used the builder pattern like descripted in Bloch Item 2. </p> <p>Yesterday I changed some small detail. I added a public constructor for the default values. So you could use both the Builder for a complex object or a constructor for simple object with the necessary values.</p> <pre><code>public class Blub { private final String id; public Blub( final String id ) { this( Blub.Builder(id) ); } private Blub( Builder builder ) { this.id = builder.id; } public static class Builder { private final String id; public Builder( final String id ) { this.id = id; } public Blub build() { return new Blub(this); } } } </code></pre> <p>But I am not sure if this has a design flaw. Because if I finalize the class I'm sure there are no disadvantages. So in the easy case, it would be possible to do call the Blub constructor.</p> <pre><code>new Blub("1a"); </code></pre> <p>instead of </p> <pre><code>(new Blub.Builder("1a")).build(); </code></pre> <p>But if I don't finalize the class it would be possible to extend it and do all kind of stuff in the new constructor. And I am not sure if there are no cases to mess this up now. Any idea?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload