Last active
July 4, 2016 17:20
-
-
Save calimaborges/1faae6c4c838af981ef50e24258510da to your computer and use it in GitHub Desktop.
IoC and Factory
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 TarefaRepository { | |
public List<Tarefa> listaTarefas() { | |
return new ArrayList<Tarefa>() {{ | |
add(new Tarefa("titulo 1", "descricao 1")) | |
add(new Tarefa("titulo 2", "descricao 2")) | |
}}; | |
} | |
} | |
class TarefaViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(); | |
public String show() { | |
return Render.view(tarefaRepository.listaTarefas()) | |
} | |
} |
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 TarefaRepository { | |
private void List<Tarefa> tarefas = new ArrayList<Tarefa>() {{ | |
add(new Tarefa("titulo 1", "descricao 1")) | |
add(new Tarefa("titulo 2", "descricao 2")) | |
}}; | |
public List<Tarefa> listaTarefas() { | |
return tarefas; | |
} | |
public Tarefa detalharTarefa(Long id) { | |
for (Tarefa tarefa : tarefas) { | |
if (tarefa.id == id) return tarefa; | |
} | |
return null; | |
} | |
} | |
class TarefaViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(); | |
public String show() { | |
return Render.view("lista-tarefas", tarefaRepository.listaTarefas()) // view renderizada faz chamada para TarefaDetalheViewController | |
} | |
} | |
class TarefaDetalheViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(); | |
public String show() { | |
return Render.view("detalhamento-tarefa", tarefaRepository.detalharTarefa(Render.viewParam("id"))) | |
} | |
} |
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 TarefaRepository { | |
private Connection connection = new Connection("db://127.0.0.1:3456/base_dados"); | |
public List<Tarefa> listaTarefas() { | |
return connection.do("select * from tarefas").list(Tarefa.class); | |
} | |
public Tarefa detalharTarefa(Long id) { | |
return connection.do("select * from tarefas where id = :id").param("id", id).one(Tarefa.class); | |
} | |
} | |
class TarefaViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(); | |
public String show() { | |
return Render.view("lista-tarefas", tarefaRepository.listaTarefas()) // view renderizada faz chamada para TarefaDetalheViewController | |
} | |
} | |
class TarefaDetalheViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(); | |
public String show() { | |
return Render.view("detalhamento-tarefa", tarefaRepository.detalharTarefa(Render.viewParam("id"))) | |
} | |
} |
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 UsuarioRepository { | |
private Connection connection = new Connection("db://127.0.0.1:3456/base_dados"); | |
// começa a ficar uma merda ter que repetir o connection | |
public List<Usuario> listaUsuarios() { | |
return connection.do("select * from usuarios").list(Usuario.class); | |
} | |
public Usuario detalharUsuario(Long id) { | |
return connection.do("select * from usuarios where id = :id").param("id", id).one(Usuario.class); | |
} | |
} | |
class TarefaRepository { | |
private Connection connection = new Connection("db://127.0.0.1:3456/base_dados"); | |
// começa a ficar uma merda ter que repetir o connection | |
public List<Tarefa> listaTarefas() { | |
return connection.do("select * from tarefas").list(Tarefa.class); | |
} | |
public Tarefa detalharTarefa(Long id) { | |
return connection.do("select * from tarefas where id = :id").param("id", id).one(Tarefa.class); | |
} | |
} | |
class TarefaViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(); | |
public String show() { | |
return Render.view("lista-tarefas", tarefaRepository.listaTarefas()) // view renderizada faz chamada para TarefaDetalheViewController | |
} | |
} | |
class TarefaDetalheViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(); | |
public String show() { | |
return Render.view("detalhamento-tarefa", tarefaRepository.detalharTarefa(Render.viewParam("id"))) | |
} | |
} |
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 UsuarioRepository { | |
private Connection connection; | |
public UsuarioRepository(Connection connection) { | |
this.connection = connection; | |
} | |
public List<Usuario> listaUsuarios() { | |
return connection.do("select * from usuarios").list(Tarefa.class); | |
} | |
public Usuario detalharUsuario(Long id) { | |
return connection.do("select * from usuarios where id = :id").param("id", id).one(Usuario.class); | |
} | |
} | |
class TarefaRepository { | |
private Connection connection; | |
public TarefaRepository(Connection connection) { | |
this.connection = connection; | |
} | |
public List<Tarefa> listaTarefas() { | |
return connection.do("select * from tarefas").list(Tarefa.class); | |
} | |
public Tarefa detalharTarefa(Long id) { | |
return connection.do("select * from tarefas where id = :id").param("id", id).one(Tarefa.class); | |
} | |
} | |
class TarefaViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(new Connection("db://127.0.0.1:3456/base_dados");); | |
// ALTERAR AQUI. MANTIVEMOS O PROBLEMA DE DEFINIR A CONEXÃO EM VÁRIOS LUGARES | |
public String show() { | |
return Render.view("lista-tarefas", tarefaRepository.listaTarefas()) // view renderizada faz chamada para TarefaDetalheViewController | |
} | |
} | |
class TarefaDetalheViewController { | |
private TarefaRepository tarefaRepository = new TarefaRepository(new Connection("db://127.0.0.1:3456/base_dados");); | |
// ALTERAR AQUI TAMBEM. MANTIVEMOS O PROBLEMA DE DEFINIR A CONEXÃO EM VÁRIOS LUGARES | |
public String show() { | |
return Render.view("detalhamento-tarefa", tarefaRepository.detalharTarefa(Render.viewParam("id"))) | |
} | |
} |
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 UsuarioRepository { | |
private Connection connection; | |
public UsuarioRepository(Connection connection) { | |
this.connection = connection; | |
} | |
public List<Usuario> listaUsuarios() { | |
return connection.do("select * from usuarios").list(Tarefa.class); | |
} | |
public Usuario detalharUsuario(Long id) { | |
return connection.do("select * from usuarios where id = :id").param("id", id).one(Usuario.class); | |
} | |
} | |
class TarefaRepository { | |
private Connection connection; | |
public TarefaRepository(Connection connection) { | |
this.connection = connection; | |
} | |
public List<Tarefa> listaTarefas() { | |
return connection.do("select * from tarefas").list(Tarefa.class); | |
} | |
public Tarefa detalharTarefa(Long id) { | |
return connection.do("select * from tarefas where id = :id").param("id", id).one(Tarefa.class); | |
} | |
} | |
class TarefaViewController { | |
// Eu costumo colocar as Factories somente em pontos de entrada | |
// No caso do Spark eu nem crio factory, o único ponto de entrada é o App.main mesmo. | |
// No Spark instancio as classes no App e saio passando elas como dependência do que for necessário | |
private Factory factory = Factory.getInstance(); | |
public String show() { | |
return Render.view("lista-tarefas", factory.getTarefaRepository().listaTarefas()) // view renderizada faz chamada para TarefaDetalheViewController | |
} | |
} | |
class TarefaDetalheViewController { | |
// Eu costumo colocar as Factories somente em pontos de entrada | |
// No caso do Spark eu nem crio factory, o único ponto de entrada é o App.main mesmo. | |
// No Spark instancio as classes no App e saio passando elas como dependência do que for necessário | |
private Factory factory = Factory.getInstance(); | |
public String show() { | |
return Render.view("detalhamento-tarefa", factory.getTarefaRepository().detalharTarefa(Render.viewParam("id"))) | |
} | |
} | |
class Factory { | |
private Connection connection; | |
private TarefaRepository tarefaRepository; | |
public Factory() { | |
this.connection = new Connection("db://127.0.0.1:3456/base_dados"); | |
this.tarefaRepository = new TarefaRepository(connection); | |
} | |
public Connection getConnection() { | |
return this.connection; | |
} | |
public TarefaRepository getTarefaRepository() { | |
return this.tarefaRepository; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment