Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC - do I need to use Controller in the View?
    text
    copied!<p>As I know in the standard implementation of the MVC we pass Controller and Model to the View</p> <p>But Im a little bit disagree with this idea. I dont want my view to know about both controller and model (oh no. maybe sometimes view needs model, but I'm sure that he can live without knowledge of controller)</p> <p>In my opinion Controller should manage View and Model, and Model doesn't need to know about controller and view; view doesnt need to know controller (I dont exclude model because some implementations of views need to know about model to listen to changes in the model). So my idea is that <strong>view doesnt need to know about controller</strong>.</p> <p><strong>1.</strong> Here is one example:</p> <pre><code>public class MyView implements ButtonClickListener { private Controller myController; private Button myButton; // I commented out the model because we dont need it now // we are talking about using controller in the view public MyView(Controller c/*, Model m*/) { myController = c; myButton = new Button(); // lets say that it is "register" button myButton.setOnButtonClickListener(this); } public void setRegisterButtonText(String text) { myButton.setText(text); } @Override public void onClick() { myController.tellToModelToDoSomething(); } } </code></pre> <p>And Controller:</p> <pre><code>public MyController implements Controller { private Model model; private View view; public MyController(Model model) { this.model = model; this.view = new MyView(this); } public void tellToModelToDoSomething() { model.doSomeActions(); } } </code></pre> <p><strong>2.</strong> And now how do I see this implementation without passing the controller:</p> <p>My View:</p> <pre><code>public class MyView { private Button myButton; public MyView() { myButton = new Button(); } public void setRegisterButtonText(String text) { myButton.setText(text); } public void setOnRegisterButtonClick(final Command command) { myButton.setOnButtonClickListener(new ButtonClickListener() { @Override public void onClick() { command.execute(); } }); } } </code></pre> <p>"Command" interface:</p> <pre><code>public interface Command { void execute(/*also can handle extra params*/); } </code></pre> <p>And Controller:</p> <pre><code>public MyController implements Controller { private Model model; private View view; public MyController(Model model) { this.model = model; this.view = new MyView(); view.setOnRegisterButtonClick(command); } public void tellToModelToDoSomething() { model.doSomeActions(); } private Command command = new Command() { public void execute() { tellToModelToDoSomething(); } }; </code></pre> <p>}</p> <p>So why do I think that using controller in the view <strong>IS NOT GOOD</strong>:</p> <p>We are mixing controller and view implementations, making new dependencies.</p> <p>Also I think that View should contain only VIEWS and operations with them (and using controller and some of his methods already looks like logic).</p> <p>In the first example view tells controller what to do. Are you agree? It looks like view controls the controller!</p> <p>In the second example controller controls what to do and just says to the view what to do if some button (only view knows what button it will be) clicked </p> <p>I always used the second scheme, but after reading a new one book about mvc, that says that we need to pass the controller to the view, I'm a little bit confusing.</p> <p>Can you please help me to understand why am I wrong and show me some examples?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload