java
  • Introduction
  • Patrones de Diseño
    • Builder Design Pattern
  • Java 8
    • Ordenar List
    • Optional
    • Gradle
    • filter
    • Lambda y Stream
  • Java 9
  • Java 10
  • Java 11
  • Java 12
  • Performance
  • Modularidad
  • GraalVM
    • Graalvm
  • JUnit
  • Apache Archiva
    • Apache Archiva
  • Dropbox APi
  • JVM Configuracion
    • JVM Configuration
    • NetBeans.conf
    • Glassfish
  • Data Science in Pure Java
  • Reflection
    • Call Methods at Runtime Using Java Reflection
  • LambdaMetaFactory
Powered by GitBook
On this page
  • Implementacion
  • Crear la clase con Builder dentro del beans

Was this helpful?

  1. Patrones de Diseño

Builder Design Pattern

PreviousPatrones de DiseñoNextJava 8

Last updated 6 years ago

Was this helpful?

Fuente

  • 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

Implementacion

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

Crear la clase con Builder dentro del beans

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

    }

}
https://www.baeldung.com/creational-design-patterns