# Builder

**Fuente**

<https://www.baeldung.com/creational-design-patterns>

* Se usa cuando la creación de un objeto es compleja.
* El patrón permite usar otro objeto Builder para construir el objeto.
* Defines una clase dentro de entity con los atributos
* Creas métodos Builder para cada atributo

![](/files/-Lc1X8joWka0Jp2mX0QX)

## Implementacion

```java
 Persona persona = new Persona.Builder()
                .withCedula("7-7-7")
                .withNombre("Aristides")
                .withEdad(5)
                .build();
```

## Crear la clase con Builder dentro del beans

```java
public class Persona {

    private String cedula;
    private String nombre;
    private Integer edad;

    public Persona() {
    }

    public Persona(String cedula, String nombre, Integer edad) {
        this.cedula = cedula;
        this.nombre = nombre;
        this.edad = edad;
    }

    public static class Builder {

        private String cedula;
        private String nombre;
        private Integer edad;

        public Builder withCedula(String cedula) {
            this.cedula = cedula;
            return this;
        }
        public Builder withNombre(String nombre) {
            this.nombre = nombre;
            return this;
        }

        public Builder withEdad(Integer edad) {
            this.edad = edad;
            return this;
        }

        public Persona build() {
            return new Persona(cedula, nombre, edad);
        }

    }

}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://avbravo-2.gitbook.io/trucosjakartaee/patrones-de-diseno/builder.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
