Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Builder pattern

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.

like image 416
Jonathan Viccary Avatar asked Aug 17 '14 04:08

Jonathan Viccary


People also ask

What is a builder pattern in Java?

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.

What is the use of builder pattern?

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.

Is Singleton a builder pattern?

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.

Should I use builder pattern?

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.


1 Answers

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.

like image 181
ethanfar Avatar answered Nov 04 '22 21:11

ethanfar