2
In MVC a View serves to handle the entire user interface, the Model serves to contain the classes that represent the system and its business rules and the Controller performs the communication between the View and the Model (something like controlling the data flow), following this line of reasoning I can conclude that both the View and the Model do not talk to each other.
Follow a short example in Java to contextualize:
Viewpessoacadastro class:
public class ViewPessoaCadastro extends JFrame {
public ViewPessoaCadastro() {
initComponents();
}
private void Salvar(ActionEvent evt) {//Clique
//Salvar os dados.
}
private void Listar(ActionEvent evt) {//Clique
//Obtem todas as pessoas cadastradas e exibi para o usuario.
}
}
Class Controllerpessoa:
public class ControllerPessoa {
Pessoa pessoa;
public ControllerPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
public void salvar() {
pessoa.salvar();
}
public void alterar() {
pessoa.alterar();
}
public List<Pessoa> listarTodas() {
List<Pessoa> pessoas = pessoa.listarTodas();
return pessoas;
}
public List<String> obterErrosValidacao() {
List<String> errosValidacao = pessoa.validar();
return errosValidacao;
}
}
Classe Pessoa:
public class Pessoa {
private String nome;
private int idade;
public Pessoa() { }
public Pessoa(String nome, int idade) {
this.nome = nome;
this.idade = idade;
}
public void salvar() {
//Rotina para salvar no banco de dados.
}
public void alterar() {
//Rotina para registrar a alteração dos dados no banco de dados.
}
public List<Pessoa> listarTodas() {
//Rotina para listar todas as pessoas salvar no banco de dados.
...
}
public List<String> validar() {
//Rotina para validar a classe pessoa (Regras de negocio).
...
}
/*Getters e Setters*/
public String getNome() { return nome; }
public void setNome(String nome) { this.nome = nome; }
public int getIdade() { return idade; }
public void setIdade(int idade) { this.idade = idade; }
}
Doubts
Considering the above example in Java I came up with the following question:
Where I fill the class attributes Pessoa
, I do this in the view by creating a type object Pessoa
and then pass it on to the class builder ControllerPessoa
or I create a method in class ControllerPessoa
containing all parameters representing class attributes Pessoa
? Considering the above question there is some possibility of View talking directly with Model?
PS:They can give examples in other languages too, but preferably could be Java itself.
is usually a function of
helpers
make this view communication with model.– Daniel Omine
@Danielomine the helpers are a fourth layer?
– gato
What technology are we talking about? I know it’s in Java, but what framework MVC?
– Leonel Sanches da Silva
Dener, yes, is a "Presentation layer". Do not confuse with "service layer".
– Daniel Omine
@Ciganomorrisonmendez no technology, if you want you can cite some MVC framework :)
– gato
@Danielomine did not know that there were other layers besides the three main.
– gato
MVC is something more generic. There are several variations such as MVVM, PM, MVP, etc. For beginners, there is no need to go so deep as it will only complicate understanding. You will know what standard to adopt when needs arise.
– Daniel Omine
@Danielomine can MVC be used without these other patterns? If yes, this is how I can collect information from the view and send it to the model so that the information is persisted in some database?
– gato
For now, forget these other patterns from the previous comment. On the view take something from the database, use Gof (Observer standard). See reply from Andrew
– Daniel Omine