Y
Y
Ytsu Ytsuevich2015-06-16 23:47:02
Design patterns
Ytsu Ytsuevich, 2015-06-16 23:47:02

MVC. Can anything be public?

As a language, the path will be Java , because now I'm developing mob. app.
Let's fantasize...
Let there be some entity, Man:

public class Human {
  int id;
  String name;
  Date birthday;
  // getters and setters
}

Further, there is a model that climbs somewhere and gets data about people.
public class Model {
  public Human[] getHumans() {   /* тут куда-то лезет и что-то отдаёт */  }
  public Human getHumanById(int id) {   /* тут тоже */  }
}

And there is also a view that displays a list of people:
public class View {
   public void showHumans(Human[] humans) { /* тут как-то отображает */ }
}

And accordingly, the controller, which takes from one, gives to another, I will not describe it, so it's clear.
It seems good, but here, in my opinion (not sure) , the MVC principle collapses , tk. the same Human class is known to everyone, from the model to the view. If you believe the literature, various articles, etc., then something is wrong here, because everything is connected with a certain class (Human). But personally, this approach seems normal to me, because. if a new field is added to Human, then one way or another, the model and view will have to be corrected (because now, this is new, you need to get (Model) and also show the user (View)).
You can, as an option, take data as a string from the model (or an array of strings), parse, call View methods by passing the appropriate parameters. But it's not OOPand it doesn't look nice either.
I hope I don't have any mistakes in understanding the pattern (if there are, please wait...).
Am I right? If not (which is quite likely), then please show the correct approach.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Emin, 2015-06-17
@kofon

Nothing collapses, Human is an entity (POJO). In MVC, she can participate in all 3 parts. Or it may not participate if, for example, at the Model level you operate with some entities, and in the View you pass others, for example, composite ones.

You can, as an option, take data as a string from the model (or an array of strings), parse, call View methods by passing the appropriate parameters. But it's not OOP, and it doesn't look nice either.
OOP is not only MVC, but a programming approach in general. In addition to MVC, there is also MVP, MVVM, etc.

F
FoxInSox, 2015-06-17
@FoxInSox

Use an interface instead of a concrete class if you feel like it:

public interface IHuman{
  String getName();
  void setName(String name);
}

Wrap the view in an interface in the same way, because in client-side UI development, this is often the most complex and ever-evolving part.
ps but don't bother your head with patterns at first. They are best understood in practice, and not in "someone told" practice, but in your own.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question