> For the complete documentation index, see [llms.txt](https://avbravo-2.gitbook.io/java-8/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://avbravo-2.gitbook.io/java-8/patrones-de-diseno/builder.md).

# Builder Design Pattern

**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/-Lc1WtMWYTVIblMdMz1L)

## 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);
        }

    }

}
```
