0% found this document useful (0 votes)
2 views

Builder Design Pattern

The Builder design pattern is a creational pattern that separates the construction of complex objects from their representation, allowing for flexible and customizable object creation. It consists of three main components: Director, Builder, and Product, where the Builder handles the creation and assembly of the Product. An example in Java illustrates how a User object can be constructed using a UserBuilder, enabling the specification of parameters in a more manageable way.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Builder Design Pattern

The Builder design pattern is a creational pattern that separates the construction of complex objects from their representation, allowing for flexible and customizable object creation. It consists of three main components: Director, Builder, and Product, where the Builder handles the creation and assembly of the Product. An example in Java illustrates how a User object can be constructed using a UserBuilder, enabling the specification of parameters in a more manageable way.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Builder Design Pattern

The Builder design pattern is a creational design pattern that separates


the construction of a complex object from its representation so that the
same construction process can create different representations. It is
used to create objects that are composed of other objects, where the
creation of the composed object is a complex process, and the user
may need to specify different parameters for the creation of different
objects. The Builder pattern provides a solution for this problem by
using a separate object called a Builder to create and configure the
object being constructed.

The Builder pattern consists of the following components:

1. Director: The Director is responsible for managing the construction


process. It uses the Builder object to build the product.

2. Builder: The Builder is responsible for creating the parts that make up
the product, and for assembling those parts into the final product.

3. Product: The Product is the complex object that is being constructed.


It is composed of several smaller objects created by the Builder.

The Builder pattern is useful when the process for creating a complex
object is time-consuming or complex, and the object can be created in
multiple ways with different configurations. By separating the
construction process from the representation of the object, the Builder
pattern allows the user to create objects in a more flexible and
customizable way.

Here is an example of the Builder pattern in Java:

```
public class User {
private String firstName;
private String lastName;
private int age;
private String address;
private String email;

private User(UserBuilder builder) {


this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.address = builder.address;
this.email = builder.email;
}

public static class UserBuilder {


private String firstName;
private String lastName;
private int age;
private String address;
private String email;

public UserBuilder(String firstName, String lastName) {


this.firstName = firstName;
this.lastName = lastName;
}

public UserBuilder setAge(int age) {


this.age = age;
return this;
}

public UserBuilder setAddress(String address) {


this.address = address;
return this;
}

public UserBuilder setEmail(String email) {


this.email = email;
return this;
}

public User build() {


return new User(this);
}
}
}
```

In this example, we have a User class with private fields for firstName,
lastName, age, address, and email. We also have a private constructor
that takes a UserBuilder object as a parameter, and sets the fields of
the User object to the values specified in the UserBuilder.

The UserBuilder is a static inner class that has fields for firstName,
lastName, age, address, and email, along with setter methods for each
field. It also has a build method that returns a new User object
constructed using the fields in the UserBuilder.

Using this design pattern, a user can be created as follows:

```
User user = new User.UserBuilder("John", "Doe")
.setAge(30)
.setAddress("123 Main St.")
.setEmail("[email protected]")
.build();
```
This creates a new User object with the firstName "John", lastName
"Doe", age 30, address "123 Main St.", and email
"[email protected]". The UserBuilder allows for the creation of a
customizable User object without having to specify all the parameters
in the constructor.

You might also like