Last active
March 16, 2017 10:05
-
-
Save jeffersontpadua/178f19d2824c30924fc0a67f011b0d15 to your computer and use it in GitHub Desktop.
Interface para o padrão Builder
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
public interface ContratoBuilder<T> { | |
ContratoBuilder from(T object); | |
T build(); | |
} |
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 implements ContratoBuilder<Pessoa>{ | |
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