Why do we need to use the builder design pattern when we can do the same thing with setters? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!When would you use the Builder Pattern?On design patterns: When should I use the singleton?When would you use the Builder Pattern?What is the difference between Builder Design pattern and Factory Design pattern?Wrong ordering in generated table in jpaHow to deserialize a list using GSON or another JSON library in Java?org.springframework.orm.hibernate3.HibernateQueryException - HibernateTemplateOGNL setValue target is nullLombok javafx propertiesHibernate : Why FetchType.LAZY-annotated collection property eagerly loading?How to implement parcelable with my custom class containing Hashmap and SparseArray?

As a beginner, should I get a Squier Strat with a SSS config or a HSS?

Can an alien society believe that their star system is the universe?

How do I use the new nonlinear finite element in Mathematica 12 for this equation?

Amount of permutations on an NxNxN Rubik's Cube

Question about debouncing - delay of state change

Drawing without replacement: why the order of draw is irrelevant?

Are all finite dimensional hilbert spaces isomorphic to spaces with Euclidean norms?

A term for a woman complaining about things/begging in a cute/childish way

The code below, is it ill-formed NDR or is it well formed?

An adverb for when you're not exaggerating

Do I really need to have a message in a novel to appeal to readers?

Is CEO the "profession" with the most psychopaths?

Find 108 by using 3,4,6

Why is Nikon 1.4g better when Nikon 1.8g is sharper?

Why wasn't DOSKEY integrated with COMMAND.COM?

How were pictures turned from film to a big picture in a picture frame before digital scanning?

How does light 'choose' between wave and particle behaviour?

Performance gap between bool std:vector and array

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

NumericArray versus PackedArray in MMA12

How could we fake a moon landing now?

If windows 7 doesn't support WSL, then what does Linux subsystem option mean?

Why is it faster to reheat something than it is to cook it?

Using et al. for a last / senior author rather than for a first author



Why do we need to use the builder design pattern when we can do the same thing with setters?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!When would you use the Builder Pattern?On design patterns: When should I use the singleton?When would you use the Builder Pattern?What is the difference between Builder Design pattern and Factory Design pattern?Wrong ordering in generated table in jpaHow to deserialize a list using GSON or another JSON library in Java?org.springframework.orm.hibernate3.HibernateQueryException - HibernateTemplateOGNL setValue target is nullLombok javafx propertiesHibernate : Why FetchType.LAZY-annotated collection property eagerly loading?How to implement parcelable with my custom class containing Hashmap and SparseArray?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








6















public class Employee 
private String name;
private String address;
private int id;

public Employee()
// TODO Auto-generated constructor stub


@Override
public String toString()
return "Employee [name=" + name + ", address=" + address + ", id=" + id + "]";


public String getName()
return name;


public void setName(String name)
this.name = name;


public String getAddress()
return address;


public void setAddress(String address)
this.address = address;


public int getId()
return id;


public void setId(int id)
this.id = id;




public class Main
public static void main(String[] args)
Employee e = new Employee();
e.setName("Priyanka");
Employee e1 = new Employee();
e1.setName("Rahul");
e1.setAddress("Delhi");
System.out.println("Value of e :"+ e);
System.out.println("Value of e1:"+ e1);











share|improve this question
























  • Possible duplicate of When would you use the Builder Pattern?

    – jaco0646
    4 mins ago

















6















public class Employee 
private String name;
private String address;
private int id;

public Employee()
// TODO Auto-generated constructor stub


@Override
public String toString()
return "Employee [name=" + name + ", address=" + address + ", id=" + id + "]";


public String getName()
return name;


public void setName(String name)
this.name = name;


public String getAddress()
return address;


public void setAddress(String address)
this.address = address;


public int getId()
return id;


public void setId(int id)
this.id = id;




public class Main
public static void main(String[] args)
Employee e = new Employee();
e.setName("Priyanka");
Employee e1 = new Employee();
e1.setName("Rahul");
e1.setAddress("Delhi");
System.out.println("Value of e :"+ e);
System.out.println("Value of e1:"+ e1);











share|improve this question
























  • Possible duplicate of When would you use the Builder Pattern?

    – jaco0646
    4 mins ago













6












6








6


2






public class Employee 
private String name;
private String address;
private int id;

public Employee()
// TODO Auto-generated constructor stub


@Override
public String toString()
return "Employee [name=" + name + ", address=" + address + ", id=" + id + "]";


public String getName()
return name;


public void setName(String name)
this.name = name;


public String getAddress()
return address;


public void setAddress(String address)
this.address = address;


public int getId()
return id;


public void setId(int id)
this.id = id;




public class Main
public static void main(String[] args)
Employee e = new Employee();
e.setName("Priyanka");
Employee e1 = new Employee();
e1.setName("Rahul");
e1.setAddress("Delhi");
System.out.println("Value of e :"+ e);
System.out.println("Value of e1:"+ e1);











share|improve this question
















public class Employee 
private String name;
private String address;
private int id;

public Employee()
// TODO Auto-generated constructor stub


@Override
public String toString()
return "Employee [name=" + name + ", address=" + address + ", id=" + id + "]";


public String getName()
return name;


public void setName(String name)
this.name = name;


public String getAddress()
return address;


public void setAddress(String address)
this.address = address;


public int getId()
return id;


public void setId(int id)
this.id = id;




public class Main
public static void main(String[] args)
Employee e = new Employee();
e.setName("Priyanka");
Employee e1 = new Employee();
e1.setName("Rahul");
e1.setAddress("Delhi");
System.out.println("Value of e :"+ e);
System.out.println("Value of e1:"+ e1);








java design-patterns






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 hours ago









Boann

37.5k1291123




37.5k1291123










asked 7 hours ago









Priyanka TanejaPriyanka Taneja

82119




82119












  • Possible duplicate of When would you use the Builder Pattern?

    – jaco0646
    4 mins ago

















  • Possible duplicate of When would you use the Builder Pattern?

    – jaco0646
    4 mins ago
















Possible duplicate of When would you use the Builder Pattern?

– jaco0646
4 mins ago





Possible duplicate of When would you use the Builder Pattern?

– jaco0646
4 mins ago












3 Answers
3






active

oldest

votes


















8














The builder pattern can be useful to:



  • apply some check on the data used to initialize the object. For example if you need a double check between variables

  • create immutable objects. You can't change an object once initialized, so you can't use setters

  • add readability of code.

  • reduce the code used to initialize the object

  • have the instance in a valid state. Using setters the object instance can be in a not valid state before all the setters are called.


Note on using the builder to create immutable objects.



When you work in a multithread environment an immutable object can be shared between threads without explicit synchronization. Because the object can't change during the time is not possible to have a race condition accessing and modifying it by two threads at the same time.






share|improve this answer

























  • Why we want to create immutable objects. If at some late point of time someone wants to change the object data???

    – Priyanka Taneja
    6 hours ago











  • @PriyankaTaneja I added my comments in the answer.

    – Davide Lorenzo MARINO
    6 hours ago











  • Thanks alot @Davide

    – Priyanka Taneja
    6 hours ago











  • Also it makes it so you don't need a temp variable

    – mackycheese21
    38 mins ago


















9














There is no need to use any pattern. You can even avoid setters with making the variables public. However,




the intent of the Builder design pattern is to separate the
construction of a complex object from its representation




Source: https://en.wikipedia.org/wiki/Builder_pattern






share|improve this answer























  • ... and this is easier to use, as most Builder's methods return a reference to this to allow chaining of call. One could write: Employee jonSkeet = new Employee.Builder().withName("Jon").withLastname("Skeet").withSalary(1_000_000).build()

    – spi
    6 hours ago












  • @spi It makes code so much more easy to read and debug. Is this the only reason to use builder design pattern?

    – Priyanka Taneja
    6 hours ago











  • @PriyankaTaneja the only one no... You can also build several "Jon Skeet" just calling build() several times. Again, this may seem a minor advantage, but these little things put together makes a great difference between easy code and spaghetti code.

    – spi
    6 hours ago












  • Imagine that you had a main constructor with 10 arguments (a bad idea to start) and the fields were set with defaults. Instead of having multiple constructors for setting only certain values, you can use the Builder design pattern to set various values without confusion, while retaining the defaults for the other values.

    – jim829
    6 hours ago



















5














Using a builder pattern has a few advantages:



  1. Unlike with setters (which make your class mutable), a builder can be used to contruct immutable objects. In many cases immutable objects are preferred over mutable objects, because they are easier to understand and maintain, and because they avoid the need for locking in multithreaded environments.


  2. A builder can make sure that the object satisfies some invariants even directly after construction. For example, if your class has a name field which must never be null, the builder can check this condition and fail to construct the object when not satisfied.


Both things you can also accomplish by using a constructor which takes all the class contents as parameters, but that will be quite unreadable when your class has more than a few fields to initialize.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55748815%2fwhy-do-we-need-to-use-the-builder-design-pattern-when-we-can-do-the-same-thing-w%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    8














    The builder pattern can be useful to:



    • apply some check on the data used to initialize the object. For example if you need a double check between variables

    • create immutable objects. You can't change an object once initialized, so you can't use setters

    • add readability of code.

    • reduce the code used to initialize the object

    • have the instance in a valid state. Using setters the object instance can be in a not valid state before all the setters are called.


    Note on using the builder to create immutable objects.



    When you work in a multithread environment an immutable object can be shared between threads without explicit synchronization. Because the object can't change during the time is not possible to have a race condition accessing and modifying it by two threads at the same time.






    share|improve this answer

























    • Why we want to create immutable objects. If at some late point of time someone wants to change the object data???

      – Priyanka Taneja
      6 hours ago











    • @PriyankaTaneja I added my comments in the answer.

      – Davide Lorenzo MARINO
      6 hours ago











    • Thanks alot @Davide

      – Priyanka Taneja
      6 hours ago











    • Also it makes it so you don't need a temp variable

      – mackycheese21
      38 mins ago















    8














    The builder pattern can be useful to:



    • apply some check on the data used to initialize the object. For example if you need a double check between variables

    • create immutable objects. You can't change an object once initialized, so you can't use setters

    • add readability of code.

    • reduce the code used to initialize the object

    • have the instance in a valid state. Using setters the object instance can be in a not valid state before all the setters are called.


    Note on using the builder to create immutable objects.



    When you work in a multithread environment an immutable object can be shared between threads without explicit synchronization. Because the object can't change during the time is not possible to have a race condition accessing and modifying it by two threads at the same time.






    share|improve this answer

























    • Why we want to create immutable objects. If at some late point of time someone wants to change the object data???

      – Priyanka Taneja
      6 hours ago











    • @PriyankaTaneja I added my comments in the answer.

      – Davide Lorenzo MARINO
      6 hours ago











    • Thanks alot @Davide

      – Priyanka Taneja
      6 hours ago











    • Also it makes it so you don't need a temp variable

      – mackycheese21
      38 mins ago













    8












    8








    8







    The builder pattern can be useful to:



    • apply some check on the data used to initialize the object. For example if you need a double check between variables

    • create immutable objects. You can't change an object once initialized, so you can't use setters

    • add readability of code.

    • reduce the code used to initialize the object

    • have the instance in a valid state. Using setters the object instance can be in a not valid state before all the setters are called.


    Note on using the builder to create immutable objects.



    When you work in a multithread environment an immutable object can be shared between threads without explicit synchronization. Because the object can't change during the time is not possible to have a race condition accessing and modifying it by two threads at the same time.






    share|improve this answer















    The builder pattern can be useful to:



    • apply some check on the data used to initialize the object. For example if you need a double check between variables

    • create immutable objects. You can't change an object once initialized, so you can't use setters

    • add readability of code.

    • reduce the code used to initialize the object

    • have the instance in a valid state. Using setters the object instance can be in a not valid state before all the setters are called.


    Note on using the builder to create immutable objects.



    When you work in a multithread environment an immutable object can be shared between threads without explicit synchronization. Because the object can't change during the time is not possible to have a race condition accessing and modifying it by two threads at the same time.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 6 hours ago

























    answered 6 hours ago









    Davide Lorenzo MARINODavide Lorenzo MARINO

    19.4k22139




    19.4k22139












    • Why we want to create immutable objects. If at some late point of time someone wants to change the object data???

      – Priyanka Taneja
      6 hours ago











    • @PriyankaTaneja I added my comments in the answer.

      – Davide Lorenzo MARINO
      6 hours ago











    • Thanks alot @Davide

      – Priyanka Taneja
      6 hours ago











    • Also it makes it so you don't need a temp variable

      – mackycheese21
      38 mins ago

















    • Why we want to create immutable objects. If at some late point of time someone wants to change the object data???

      – Priyanka Taneja
      6 hours ago











    • @PriyankaTaneja I added my comments in the answer.

      – Davide Lorenzo MARINO
      6 hours ago











    • Thanks alot @Davide

      – Priyanka Taneja
      6 hours ago











    • Also it makes it so you don't need a temp variable

      – mackycheese21
      38 mins ago
















    Why we want to create immutable objects. If at some late point of time someone wants to change the object data???

    – Priyanka Taneja
    6 hours ago





    Why we want to create immutable objects. If at some late point of time someone wants to change the object data???

    – Priyanka Taneja
    6 hours ago













    @PriyankaTaneja I added my comments in the answer.

    – Davide Lorenzo MARINO
    6 hours ago





    @PriyankaTaneja I added my comments in the answer.

    – Davide Lorenzo MARINO
    6 hours ago













    Thanks alot @Davide

    – Priyanka Taneja
    6 hours ago





    Thanks alot @Davide

    – Priyanka Taneja
    6 hours ago













    Also it makes it so you don't need a temp variable

    – mackycheese21
    38 mins ago





    Also it makes it so you don't need a temp variable

    – mackycheese21
    38 mins ago













    9














    There is no need to use any pattern. You can even avoid setters with making the variables public. However,




    the intent of the Builder design pattern is to separate the
    construction of a complex object from its representation




    Source: https://en.wikipedia.org/wiki/Builder_pattern






    share|improve this answer























    • ... and this is easier to use, as most Builder's methods return a reference to this to allow chaining of call. One could write: Employee jonSkeet = new Employee.Builder().withName("Jon").withLastname("Skeet").withSalary(1_000_000).build()

      – spi
      6 hours ago












    • @spi It makes code so much more easy to read and debug. Is this the only reason to use builder design pattern?

      – Priyanka Taneja
      6 hours ago











    • @PriyankaTaneja the only one no... You can also build several "Jon Skeet" just calling build() several times. Again, this may seem a minor advantage, but these little things put together makes a great difference between easy code and spaghetti code.

      – spi
      6 hours ago












    • Imagine that you had a main constructor with 10 arguments (a bad idea to start) and the fields were set with defaults. Instead of having multiple constructors for setting only certain values, you can use the Builder design pattern to set various values without confusion, while retaining the defaults for the other values.

      – jim829
      6 hours ago
















    9














    There is no need to use any pattern. You can even avoid setters with making the variables public. However,




    the intent of the Builder design pattern is to separate the
    construction of a complex object from its representation




    Source: https://en.wikipedia.org/wiki/Builder_pattern






    share|improve this answer























    • ... and this is easier to use, as most Builder's methods return a reference to this to allow chaining of call. One could write: Employee jonSkeet = new Employee.Builder().withName("Jon").withLastname("Skeet").withSalary(1_000_000).build()

      – spi
      6 hours ago












    • @spi It makes code so much more easy to read and debug. Is this the only reason to use builder design pattern?

      – Priyanka Taneja
      6 hours ago











    • @PriyankaTaneja the only one no... You can also build several "Jon Skeet" just calling build() several times. Again, this may seem a minor advantage, but these little things put together makes a great difference between easy code and spaghetti code.

      – spi
      6 hours ago












    • Imagine that you had a main constructor with 10 arguments (a bad idea to start) and the fields were set with defaults. Instead of having multiple constructors for setting only certain values, you can use the Builder design pattern to set various values without confusion, while retaining the defaults for the other values.

      – jim829
      6 hours ago














    9












    9








    9







    There is no need to use any pattern. You can even avoid setters with making the variables public. However,




    the intent of the Builder design pattern is to separate the
    construction of a complex object from its representation




    Source: https://en.wikipedia.org/wiki/Builder_pattern






    share|improve this answer













    There is no need to use any pattern. You can even avoid setters with making the variables public. However,




    the intent of the Builder design pattern is to separate the
    construction of a complex object from its representation




    Source: https://en.wikipedia.org/wiki/Builder_pattern







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 6 hours ago









    maio290maio290

    2,222615




    2,222615












    • ... and this is easier to use, as most Builder's methods return a reference to this to allow chaining of call. One could write: Employee jonSkeet = new Employee.Builder().withName("Jon").withLastname("Skeet").withSalary(1_000_000).build()

      – spi
      6 hours ago












    • @spi It makes code so much more easy to read and debug. Is this the only reason to use builder design pattern?

      – Priyanka Taneja
      6 hours ago











    • @PriyankaTaneja the only one no... You can also build several "Jon Skeet" just calling build() several times. Again, this may seem a minor advantage, but these little things put together makes a great difference between easy code and spaghetti code.

      – spi
      6 hours ago












    • Imagine that you had a main constructor with 10 arguments (a bad idea to start) and the fields were set with defaults. Instead of having multiple constructors for setting only certain values, you can use the Builder design pattern to set various values without confusion, while retaining the defaults for the other values.

      – jim829
      6 hours ago


















    • ... and this is easier to use, as most Builder's methods return a reference to this to allow chaining of call. One could write: Employee jonSkeet = new Employee.Builder().withName("Jon").withLastname("Skeet").withSalary(1_000_000).build()

      – spi
      6 hours ago












    • @spi It makes code so much more easy to read and debug. Is this the only reason to use builder design pattern?

      – Priyanka Taneja
      6 hours ago











    • @PriyankaTaneja the only one no... You can also build several "Jon Skeet" just calling build() several times. Again, this may seem a minor advantage, but these little things put together makes a great difference between easy code and spaghetti code.

      – spi
      6 hours ago












    • Imagine that you had a main constructor with 10 arguments (a bad idea to start) and the fields were set with defaults. Instead of having multiple constructors for setting only certain values, you can use the Builder design pattern to set various values without confusion, while retaining the defaults for the other values.

      – jim829
      6 hours ago

















    ... and this is easier to use, as most Builder's methods return a reference to this to allow chaining of call. One could write: Employee jonSkeet = new Employee.Builder().withName("Jon").withLastname("Skeet").withSalary(1_000_000).build()

    – spi
    6 hours ago






    ... and this is easier to use, as most Builder's methods return a reference to this to allow chaining of call. One could write: Employee jonSkeet = new Employee.Builder().withName("Jon").withLastname("Skeet").withSalary(1_000_000).build()

    – spi
    6 hours ago














    @spi It makes code so much more easy to read and debug. Is this the only reason to use builder design pattern?

    – Priyanka Taneja
    6 hours ago





    @spi It makes code so much more easy to read and debug. Is this the only reason to use builder design pattern?

    – Priyanka Taneja
    6 hours ago













    @PriyankaTaneja the only one no... You can also build several "Jon Skeet" just calling build() several times. Again, this may seem a minor advantage, but these little things put together makes a great difference between easy code and spaghetti code.

    – spi
    6 hours ago






    @PriyankaTaneja the only one no... You can also build several "Jon Skeet" just calling build() several times. Again, this may seem a minor advantage, but these little things put together makes a great difference between easy code and spaghetti code.

    – spi
    6 hours ago














    Imagine that you had a main constructor with 10 arguments (a bad idea to start) and the fields were set with defaults. Instead of having multiple constructors for setting only certain values, you can use the Builder design pattern to set various values without confusion, while retaining the defaults for the other values.

    – jim829
    6 hours ago






    Imagine that you had a main constructor with 10 arguments (a bad idea to start) and the fields were set with defaults. Instead of having multiple constructors for setting only certain values, you can use the Builder design pattern to set various values without confusion, while retaining the defaults for the other values.

    – jim829
    6 hours ago












    5














    Using a builder pattern has a few advantages:



    1. Unlike with setters (which make your class mutable), a builder can be used to contruct immutable objects. In many cases immutable objects are preferred over mutable objects, because they are easier to understand and maintain, and because they avoid the need for locking in multithreaded environments.


    2. A builder can make sure that the object satisfies some invariants even directly after construction. For example, if your class has a name field which must never be null, the builder can check this condition and fail to construct the object when not satisfied.


    Both things you can also accomplish by using a constructor which takes all the class contents as parameters, but that will be quite unreadable when your class has more than a few fields to initialize.






    share|improve this answer



























      5














      Using a builder pattern has a few advantages:



      1. Unlike with setters (which make your class mutable), a builder can be used to contruct immutable objects. In many cases immutable objects are preferred over mutable objects, because they are easier to understand and maintain, and because they avoid the need for locking in multithreaded environments.


      2. A builder can make sure that the object satisfies some invariants even directly after construction. For example, if your class has a name field which must never be null, the builder can check this condition and fail to construct the object when not satisfied.


      Both things you can also accomplish by using a constructor which takes all the class contents as parameters, but that will be quite unreadable when your class has more than a few fields to initialize.






      share|improve this answer

























        5












        5








        5







        Using a builder pattern has a few advantages:



        1. Unlike with setters (which make your class mutable), a builder can be used to contruct immutable objects. In many cases immutable objects are preferred over mutable objects, because they are easier to understand and maintain, and because they avoid the need for locking in multithreaded environments.


        2. A builder can make sure that the object satisfies some invariants even directly after construction. For example, if your class has a name field which must never be null, the builder can check this condition and fail to construct the object when not satisfied.


        Both things you can also accomplish by using a constructor which takes all the class contents as parameters, but that will be quite unreadable when your class has more than a few fields to initialize.






        share|improve this answer













        Using a builder pattern has a few advantages:



        1. Unlike with setters (which make your class mutable), a builder can be used to contruct immutable objects. In many cases immutable objects are preferred over mutable objects, because they are easier to understand and maintain, and because they avoid the need for locking in multithreaded environments.


        2. A builder can make sure that the object satisfies some invariants even directly after construction. For example, if your class has a name field which must never be null, the builder can check this condition and fail to construct the object when not satisfied.


        Both things you can also accomplish by using a constructor which takes all the class contents as parameters, but that will be quite unreadable when your class has more than a few fields to initialize.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 6 hours ago









        HoopjeHoopje

        10.3k52644




        10.3k52644



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55748815%2fwhy-do-we-need-to-use-the-builder-design-pattern-when-we-can-do-the-same-thing-w%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480