Reputation: 361
Why can't I just do
Animal object = new Animal();
object.setType("cat");
object.setName("Meow");
instead of
Animal.Builder objectBuilder = new Animal.Builder();
Animal object = objectBuilder.setType("cat").setName("Meow").build();
or
Animal object = new Animal.Builder().setType("cat").setName("Meow").build();
All three ways end up creating an Animal
object with type cat and name Meow, no? Are the latter methods superior to the first one? If I use the first way, I don't have to create a separate Builder class and I can just use getters and setters.
Upvotes: 2
Views: 3662
Reputation: 271735
The builder pattern saves you time writing code!
Notice how you need to write the word object
many many times if you used setters to set the properties? With the builder pattern, you don't need to write object
every time. This is especially helpful if your variable names are long.
More importantly though, the builder pattern helps you create immutable objects. Immutable objects are generally considered a good thing. You see, the Builder
object is not actually immutable, but once you call build()
, the return value can be immutable. If you don't use the builder pattern and want to create something immutable, then you have to put all the property values in the constructor:
new MyObject("some string", 10, true, 0.5)
And you would have no idea what each parameter is for.
Upvotes: 3
Reputation: 11
I would use builder pattern when the object is complex and has lot of attributes. Builder pattern helps to create objects with different initialization attributes quickly. Imagine having an class with 10 attributes and every time you have to initialize all the 10 attributes. With Builder you can have default values for few attributes so that you don't have to initialize all the attributes every time.
Example:
public class AnimalBuillder {
private String category = "Domestic" // default initialization
private String type;
private String name;
AnimalBuilder withCategory(String category) {
this.category = category;
return this;
}
AnimalBuilder withName(String name) {
this.name = name;
return this;
}
AnimalBuilder withType(String type) {
this.name = name;
return this;
}
Animal build() {
return new Animal(name, type, category);
}
}
Usage would be:
new AnimalBuilder().withType("cat").withName("meow").build();
OR
new AnimalBuilder().withCategory("wild").withType("cat").withName("meow").build();
depending on how well you provide default initialization you can create objects with different attributes quickly with very few chain methods using a builder.
Upvotes: 1
Reputation: 2701
Short answer: consider the following code.
Animal object = new Animal();
object.setType("cat");
// lots of code
object.setType("dog");
Now, your cat suddenly changed into a dog. This is obviously not something you want.
You don't have to use the Builder pattern, but it's a proven Design Pattern to make an object immutable. Some languages, such as C#, provide some syntactic sugar so that you don't have to write a builder class, but the principle is the same.
Upvotes: 1