Last active
December 23, 2015 20:09
-
-
Save Chavao/6687644 to your computer and use it in GitHub Desktop.
Exemplo de Builder pattern
This file contains hidden or 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 class Funcionario { | |
private String nome; | |
private Double salario; | |
public void setSalario(Double salario) { | |
this.salario = salario; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
} |
This file contains hidden or 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
class FuncionarioBuilder { | |
private String nome; | |
private Double salario; | |
public FuncionarioBuilder comSalarioDe(Double salario) { | |
this.salario = salario; | |
return this; | |
} | |
public FuncionarioBuilder comNome(String nome) { | |
this.nome = nome; | |
return this; | |
} | |
public Funcionario construir() { | |
Funcionario f = new Funcionario(); | |
f.setSalario(this.salario); | |
f.setNome(this.nome); | |
return f; | |
} | |
} |
This file contains hidden or 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
// Como utilizar | |
FuncionarioBuilder builder = new FuncionarioBuilder(); | |
Funcionario funcionario = builder.comNome("Chavão") | |
.comSalarioDe(4500.0) | |
.construir(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment