Hacker News new | past | comments | ask | show | jobs | submit login

Picking very specifically on the builder pattern: the whole point is to solve one or more of the following problems:

1. creating complex objects and having them valid at creation

2. many optional arguments for an object's construction

3. similar types for object construction

For e.g. in Java you might see something like:

  Point p = new Point(10, 0, 5); // (x=10, z=5)
  Point q = new Point(10, 2); // (x=10, y=2)
which may be problematic because

1. y needs to be specified explicitly to 0, because you can't expose an overloaded constructor that takes x and z (that would clash with the constructor that takes x and y

2. It _may_ not be evident that new Point(10, 0, 5); passes x, y and z in that order

3. It ties the constructor to the implementation. You can never expose a Point(double radius, double theta) constructor because that would clash with the Point(double x, double y) constructor.

The builder would solve all of these problems:

  Point p = Point.newBuilder()
                 .setX(10)
                 .setZ(5)
                 .build();

Python however doesn't necessarily need this pattern because idiomatically solves all three of these problems by providing

1. named arguments, and

2. default arguments

so you could have:

  p = Point(x=10, z=5)
  q = Point(10, 2)
  t = Point(radius=5, theta=45)

I feel the post completely glosses over this by providing a single super-class with the appropriate constructor, and overriding the methods in the sub-classes. What if I just have a single class that I want a builder for? I don't think it showcases the builder pattern at all, just method overloading in classes.



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: