Created
March 14, 2017 01:44
-
-
Save jeffersontpadua/67e4fab649a3e8de6e31f56f7e48dea5 to your computer and use it in GitHub Desktop.
Padrão Builder para a criação de objetos em Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
public class Pessoa { | |
private final String nome; | |
private final String cpf; | |
private final String rg; | |
private final LocalDate dataNascimento; | |
private Pessoa(Builder builder) { | |
this.nome = builder.nome; | |
this.cpf = builder.cpf; | |
this.rg = builder.rg; | |
this.dataNascimento = builder.dataNascimento; | |
} | |
public static class Builder { | |
private String nome = ""; | |
private String cpf = ""; | |
private String rg = ""; | |
private LocalDate dataNascimento = null; | |
public Builder from(Pessoa pessoa) { | |
this.nome = pessoa.getNome(); | |
this.cpf = pessoa.getCpf(); | |
this.rg = pessoa.getRg(); | |
this.dataNascimento = pessoa.getDataNascimento(); | |
return this; | |
} | |
public Pessoa build() { | |
return new Pessoa(this); | |
} | |
public Builder setNome(String nome) { | |
this.nome = nome; | |
return this; | |
} | |
public Builder setCpf(String cpf) { | |
this.cpf = cpf; | |
return this; | |
} | |
public Builder setRg(String rg) { | |
this.rg = rg; | |
return this; | |
} | |
public Builder setDataNascimento(LocalDate dataNascimento) { | |
this.dataNascimento = dataNascimento; | |
return this; | |
} | |
} | |
public String getNome() { | |
return this.nome; | |
} | |
public String getCpf() { | |
return this.cpf; | |
} | |
public String getRg() { | |
return this.rg; | |
} | |
public LocalDate getDataNascimento() { | |
return this.dataNascimento; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment