Search code examples
javamodel-view-controllermvp

MVP How to implement simple login


I wonder how you can implement a simple login case with MVP. My MVP framework does allow to fire events from the view only, I can't fire events from the presenter. Don't know if this is a good or a bad thing. I do see advantages with it but simple cases are just blown up with additional indirections.

So when I want to implement the simple login scenario I have the following.

  • LoginPresenter
  • LoginView
  • LoginModel

So what happens when the user fills in the login form and clicks on the login button?

  1. The view fires an event "login clicked"
  2. The presenter listens to that event and does the login using it's model
  3. If the login was successufl, I have to call the view again to tell the login was successful.
  4. The view fires another event "login successful".
  5. The presenter and other presenters now show another view and close the login window.

From my point of view the step via view for the successful event is just too much...

Is my framework wrong not letting me fire events from the presenter or is this a neccessary evil of MVP?


Solution

  • Each framework will define its own rules regarding exactly how the View triggers the Presenter, or vice versa. You'll have to dig around to see if any other frameworks seem slightly easier. However, the overall effort is likely to remain similar.

    It may seem like a lot of effort for a simple process like logging in, but in my opinion the pain is worth it for the benefits of unit-testability.

    Now you can test your log in process by mocking your View and your Model. Does the Presenter handle invalid inputs correctly? Does the View get the right prompting if the log in is incorrect/correct? All these questions can now be answered with unit tests using mocked objects for your Model and View.

    Just make sure your View(s) and Model(s) are defined as interfaces. Check out libraries such as JMock for helping with your unit testing.

    Then, imagine how useful this framework would be for a more complex scenario, like processing the customer's order!