I recently wrote a builder class and noticed that the standard seems to be as follows
public class PersonBuilder {
private String firstName;
private String lastName;
public PersonBuilder withFirstName(String firstName) {
this.firstName = firstName;
return this;
}
public PersonBuilder withLastName(String lastName) {
this.lastName = lastName;
return this;
}
public Person build() {
return new Person(this);
}
}
Is there any disadvantage to, instead, doing the following
public class PersonBuilder {
private Person person;
public PersonBuilder withFirstName(String firstName) {
person.setFirstName(firstName);
return this;
}
public PersonBuilder withLastName(String lastName) {
person.setLastName(lastName);
return this;
}
public Person build() {
return person;
}
}
I understand this may be an opinion based question, I was just looking for any answers as to why this may be a bad or better design pattern.
Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.
The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.
Singleton pattern is the simplest design pattern and the factory method is supposed to be a common design pattern that is widely used. The builder pattern is used to construct complex objects and is mostly used in developing complex applications.
Builder Pattern: When to Use This pattern should be used: When it's necessary to use a constructor with a long parameter list or when there's a long list of constructors with different parameters. When it's necessary to build different representations of the same object.
There are several problems with your approach. Some of them are described in previous answers so I'll just mention the others.
The biggest problem with your design, is that you're using a single instance of Person
in the builder. This means that if you're using the same builder more than once, you'll be "building" the same instance, while the clients using it are expecting two different instances. No need to mention that this could cause some serious havoc in your application.
The answer you got from @Basilevs mentions that the "built" class will require setters. This is absolutely true, but I'd just like to stress that this is a huge problem, since it means that the classes you "build" can never be immutable ! In other words, you're restricting the implementers of such classes to using synchronization for thread safety if needed, and other problem solving mechanisms that could have been avoided using the common approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With