Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Builder pattern

  1. #1
    Member
    Join Date
    Oct 2013
    Location
    Cyprus
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Builder pattern

    I am currently reading the book effective java and there is section where a class has a private constructor and an inner class assigns parameters to the outer class.WHY to do that and when i try to create setter, getter methods to retrieve data they don't work.Below is the code:

    public class NutritionFacts {
        private final int servingSize;
        private final int servings;
        private final int calories;
        private final int fat;
        private final int sodium;
        private final int carbohydrate;
     
        public static class Builder {
            // Required parameters
            private final int servingSize;
            private final int servings;
     
            // Optional parameters - initialized to default values
            private int calories      = 0;
            private int fat           = 0;
            private int carbohydrate  = 0;
            private int sodium        = 0;
     
            public Builder(int servingSize, int servings) {
                this.servingSize = servingSize;
                this.servings    = servings;
            }
     
            public Builder calories(int val)
                { calories = val;      return this; }
            public Builder fat(int val)
                { fat = val;           return this; }
            public Builder carbohydrate(int val)
                { carbohydrate = val;  return this; }
            public Builder sodium(int val)
                { sodium = val;        return this; }
     
            public NutritionFacts build() {
                return new NutritionFacts(this);
            }
        }
     
        private NutritionFacts(Builder builder) {
            servingSize  = builder.servingSize;
            servings     = builder.servings;
            calories     = builder.calories;
            fat          = builder.fat;
            sodium       = builder.sodium;
            carbohydrate = builder.carbohydrate;
        }
    }

    HOW I WILL BE ABLE TO RETRIEVE DATA FROM THAT CLASS???WHY SOMEONE WANTS TO DO THAT?? i have learned with the public constructor i can pass the params i want and retrieve the data easily with getter methods, why to do it so complicate?


  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Builder pattern

    You're right, as it stands NutritionFacts is useless because doesn't expose any of its information. I'm guessing that's because it was simplified for the book's example, and the intent is to add more functionality, or at least field accessors where it can be passed to an algorithm to do things like calculate how much insulin should be taken. The point is, NutritionFacts has so many inputs (servingSize, servings, calories, carbs, fat, sodium) before it's state is ready, and some are optional, that creating constructors for all the possible combinations would be a trainwreck (and in this case impossible for all combinations because of int ambiguity). Maybe in the future a calculation must be ran before NutritionFacts is ready to be used. With this pattern, you don't get a NutritionFacts object without calling the build() method, which makes things a bit less error-prone.

Similar Threads

  1. window builder
    By swikot in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2014, 04:39 PM
  2. GUI Builder.
    By 344a in forum Java IDEs
    Replies: 5
    Last Post: October 25th, 2013, 06:48 AM
  3. How to use Builder Setters
    By copeg in forum Java Programming Tutorials
    Replies: 3
    Last Post: January 19th, 2012, 06:40 PM
  4. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM

Tags for this Thread