One of the issues that surround MVP in general is there are several flavors and people new to the pattern get confused because each flavor has differing advantages and disadvantages. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 10 years, 8 months ago. Active 5 years, 5 months ago. Viewed 6k times. Improve this question.
BornToCode 8, 7 7 gold badges 55 55 silver badges 76 76 bronze badges. Mark Mark 7, 11 11 gold badges 49 49 silver badges 84 84 bronze badges. I suggest GWTP code. Well, there are indeed more MVP frameworks than I thought: stackoverflow. Add a comment. Active Oldest Votes. This helps tests tremendously but it's invaluable benefit of UI design by itself.
MVP takes away the intelligence from the View and adds it to the controller which makes a Presenter. The view is completely passive and every interaction of the view with the model is done through the Presenter.
By removing intelligence or business logic from the view, we reduce risk associated with not testing the view. Usually, unit tests are only applied to business logic and the model. Our application has two views: a login page and a main page. Upon successful login, the user is directed to the main page, and on logout the user goes back to the login page. That should be as expected as the view is dumb and contains no logic. The first two chapter examples are the same, except they differ in how views are implemented.
In the first example, the implementations are verbose as we have a lot of boilerplate code; in the second, we have swapped the view implementation for UiBinder versions. The only difference between the two versions is the use of UiBinder, and therefore how we bind events to actions. The actual actions do not change. Each view also implements a reaction to the title being changed. In the UiBinder approach it is neatly described as follows.
In the non UIBinder project, the same logic is there, but it is implemented by manually creating the title object and then adding a ValueChangeEvent handler to it. By convention, we tend to put all the binding code in a bind method, if we are not using UIBinder.
So, our view is dumb and contains zero application logic. That is because we agreed that all of that should appear only in the presenter. And this brings us to the discussion of those presenters. Presenters are where all the application logic sits and will have no UI components.
In a similar way to views, we provide a generic presenter interface, a specific one, and an implementation for each presenter. Just like views, we will create a basic interface that all of our presenters will implement. Again, there is no requirement to do this; it is just good to have.
The go method takes the widget in which we wish the presenter-associated view associated to be displayed. All we require of that widget is that it implements the HasWidgets interface; in other words, it is a panel of some kind.
A presenter will also implement a bind method. In our design, this is where the presenter will listen to any application-wide events it is interested in for example, this is where it hooks into the event bus as well as where it calls the associated views setPresenter method.
In the specific presenter interface we wish to declare those methods that will be called from the view when things happen. Remember that we said the view is responsible for reacting to UI events within itself, but that it will then call methods on the presenter that was registered with it for the actual business logic. Here, we see the PhotoDetailsPresenter interface relating to the title:. What we are saying here is that we expect the onUpdateTitle method to be called when the title is updated in the view.
We would add other methods to the interface based on the other UI components that are of interest. Our presenters are just implementations of the specific presenter interfaces. This means it needs to implement the go and bind method from Presenter as well as the onUpdateTitle from PhotoDetailsPresenter. Listing 1 shows the skeleton of our implementation. The first thing that is done in the constructor is to get hold of some common resources from the ClientFactory that we previously mentioned.
This is not a requirement but makes our lives easier. For example, we grab resources such as the RPC service and the event bus - we might as well share the RPC service for efficiency, and we have to share the event bus; otherwise, it would not be system wide. Once we have grabbed our resources from the factory, we make a call to get the details of the photo just after we have fired off a system-wide ApplicationBusy event. Not shown in listing 1 is the fact that an ApplicationFree event is fired within the RPC return handling code so that the user is notified the application is no longer busy.
Within the constructor, we also call the bind method from the Presenter interface. Other implementations may register on the event bus if it has to handle application wide events.
This clears the container widget passed in as the parameter and then adds the associated view as a widget. In the application controller, we will create presenters in the following manner:. Just before we jump to the AppController, which is responsible for tying everything together and starting presenters, we want to take a little deeper look at the relationship between presenter and views we have used. At the heart of the MVP pattern is the relationship between your presenters and views.
Completely inverse to that, your views should contain nothing more than widgets, and should avoid any application logic wherever possible. For two reasons: 1 fast testing with increased code coverage and 2 maximum code reuse when porting to other Java based platforms, for instance Android written carefully in the MVP paradigm, the application logic of a GWT application can be reused and all you need to do is just replace the view component.
So how do we fix this? Given that these are continuously tested by the GWT team, doing so in our own tests is simply redundant and not required. And, where you do need to test your views, those tests should be few and far between and, in many instances, simply integration testing being driven by Selenium or some Selenium-like testing suite. Now that we agree on moving the app logic out of the view, we have to move the view logic out of the presenter.
0コメント