Note that there are some explanatory texts on larger screens.

plurals
  1. POHow dumb can MVP Views really be?
    primarykey
    data
    text
    <p>I'm trying to learn MVP, but something eludes me; if the Presenter uses the view as an interface, then the View cannot be just a simple rendering of controls. Imagine trying to write a typing-practice game, where words are randomly generated into the UI and the user must type the words as they fall down the screen.</p> <p>So the view is going to have methods like:</p> <pre><code>public interface View { addWord(String word, double x, double y); // or possibly (Word word) moveWord(String word, double distance); removeWord(String word); setScore(int score); registerKeyListener(KeyListener listener); // other stuff } </code></pre> <p>But ultimately the VIEW is going to have to be responsible for creating custom controls. A lot of code is omitted here, but hopefully this is enough to illustrate what I mean. Example:</p> <pre><code>public class SwingView { private JPanel thePanel; private Map&lt;String, WordComponent&gt; currentWords = new HashMap&lt;&gt;(); public SwingView() { thePanel = new JPanel(new WordLayout()); // other stuff } public void addWord(String word, double x, double y) { WordComponent newWord = new WordComponent(word); currentWords.put(word, newWord); Point2D.Double point = new Point2D.Double(x, y); thePanel.add(newWord, point); } public void removeWord(String word) { WordComponent theWord = currentWords.get(theWord); thePanel.remove(theWord); } } </code></pre> <p>Already the View implementation has logic going on. It's maintaining a <code>Map</code> of its <code>WordComponent</code>s. I have two classes of my own here, <code>WordLayout implements LayoutManager2</code>, and <code>WordComponent extends JLabel</code> (or something else, but that would be even MORE code). </p> <p>In theory, the presenter should know nothing about Swing, so I can unit test with a mock that maybe logs to the console or something. But simply managing the Swing objects is a job into itself. Or, what if I wanted to transform this application into a Tomcat Webpage. Now class <code>ServletView</code> is managing the AJAX calls that move the words around. Which has a dependency on the AJAX framework, which is even more work offloaded to the <code>View</code>.</p> <p>Summary: Are <code>View</code> implementations supposed to have "logic" that manage their own components?</p> <p>Followup: The code I wrote above probably won't even be responsive, because the <code>Model</code> and the <code>Presenter</code> aren't working on the Event Dispatch thread (or, they are, which is probably worse). Where does the code that hands off display updates to the Event Dispatch thread go? Or, should the <code>Presenter</code> be on the Event Dispatch thread?</p> <p><strong>Edit:</strong> One idea just occurred to me. Having a platform-specific sub-presenter that <em>is</em> aware of implementation details like whether you're using Swing or something else.</p> <p><strong>Edit2:</strong> Still another question, based on @DuncanJones answer. Imagine I wanted to put logic to make the game resizeable and scale the size of everything based on the new size. Would that logic be in the <code>View</code>, or in the <code>Presenter</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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