2
$\begingroup$

The Builder Pattern solves the problem of Telescoping Constructors; that is, having a single constructor for every possible parameter combination (including those combinations for which a parameter is omitted).

So for example:

  public Food(int id, String name) {
    this(id, name, 0, 0, 0);
  }

  public Food(int id, String name, int calories) {
    this(id, name, calories, 0, 0);
  }

  public Food(int id, String name, int servingSize) {
    this(id, name, 0, servingSize, 0);
  }

  public Food(int id, String name, int fat) {
    this(id, name, 0, 0, fat);
  }

  public Food(int id, String name, int calories, int servingSize) {
    this(id, name, calories, servingSize, 0);
  }
  public Food(int id, String name, int calories, int fat) {
    this(id, name, calories, 0, fat);
  }

  public Food(int id, String name, int servingSize, int fat) {
    this(id, name, 0, servingSize, fat);
  }

  public Food(int id, String name, int calories, int servingSize, int fat) {
    this(id, name, calories, servingSize, fat);
  }

If you set the Builder Pattern up properly, using the voluminous amount of code required, you can simply:

Food food = new FoodBuilder().SetName("Bananas").SetCalories(120).Build();

using any combination of parameters in any order.

Assuming that each parameter in the constructor will always reside at the same relative location while still allowing for omitted parameters, how would one calculate the number of constructors required given the number n of optional parameters needed?

(in the above example, id and name are required parameters, while calories, servingSize, and fat are all optional parameters).

$\endgroup$
0

1 Answer 1

3
$\begingroup$

You'll need $2^n$ constructors. You need one constructor for each possible subset of optional parameters. There are $2^n$ such subsets. You have $n$ things, and there are $2^n$ possible ways to choose a set containing some number of those things.

For each subset of optional parameters, you'll write down a constructor for that subset; the arguments to that constructor will be fully determined (they must include all of the required parameters, and all of the optional parameters in the chosen subset, in the specified order).


That said, in Java, the "create many constructors" pattern doesn't actually work, for reasons having nothing to do with combinatorics or the number of constructors. You might notice that the type signature of the following two constructors is identical:

public Food(int id, String name, int calories, int servingSize) { ... }
public Food(int id, String name, int calories, int fat) { ... }

and that's not allowed in Java, because when the compiler sees an attempt to invoke the constructor (to instantiate the object) with a list of 4 parameters of type int, String, int, int, it won't know which of those two choices to choose. The approach only works if all of the optional parameters have different, incompatible types.

$\endgroup$
1
  • $\begingroup$ Good point about the method polymorphism. $\endgroup$ Apr 6, 2017 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.