This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Java in General and the fly likes Convert from a Constructor to a Builder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert from a Constructor to a Builder" Watch "Convert from a Constructor to a Builder" New topic
Author

Convert from a Constructor to a Builder

Daniel Afonso
Greenhorn

Joined: Jan 27, 2011
Posts: 16

Hi have the following code:




How would I convert this to a Builder. I'm pretty new on this.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44439
    
  33
Several ways to do it. You will have to consider whether you want to permit any changes to those fields. You should also consider whether it is wise to have so many parameters for your constructor; for one thing it makes the code hard to read, and I had to insert some line breaks.That is one way to do it. You can set up lots of methods for different combinations.
  • In that case they all have the structure Address a = getBasicAddress(...); ... return a;
  • The getBasicAddress method requires postCode and town be not null, and other null fields are changed to "", so there are no nulls.
  • You can do validation of the fields in those methods, eg with a regex for post codes.
  • There are other ways to do it, too.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Convert from a Constructor to a Builder
     
    It's not a secret anymore!