Skip to content

Instantly share code, notes, and snippets.

@mtov
Created September 19, 2019 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtov/cb98612474f01eccc5e10fa08f82a62b to your computer and use it in GitHub Desktop.
Save mtov/cb98612474f01eccc5e10fa08f82a62b to your computer and use it in GitHub Desktop.
Builder (Design Pattern)
class Livro {
private String nome;
private String autores;
private String editora;
private String ano;
private Livro (String nome, String autores, String editora, String ano) {
this.nome = nome;
this.autores = autores;
this.editora = editora;
this.ano = ano;
}
public String toString() {
return nome + ". " + autores + "," + editora + "," + ano;
}
public static class Builder {
private String nome;
private String autores;
private String editora;
private String ano;
public Builder setNome(String nome) {
this.nome = nome;
return this;
}
public Builder setAutores(String autores) {
this.autores = autores;
return this;
}
public Builder setEditora(String editora) {
this.editora = editora;
return this;
}
public Builder setAno(String ano) {
this.ano = ano;
return this;
}
public Livro build() {
return new Livro(nome, autores, editora, ano);
}
}
}
public class Main {
public static void main(String [] args) {
Livro esm = new Livro.Builder()
.setNome("Engenharia Soft Moderna")
.setEditora("UFMG")
.setAno("2020")
.build();
System.out.println("Livro 1: " + esm.toString());
Livro gof = new Livro.Builder()
.setNome("Design Patterns")
.setAutores("GoF")
.setAno("1995")
.build();
System.out.println("Livro 2: " + gof.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment