GWT MVP与Places&活动 - 模型在哪里? [英] GWT MVP with Places & Activities - Where's the Model?

查看:87
本文介绍了GWT MVP与Places&活动 - 模型在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图熟悉GWT开发的Places& Activities设计模式,到目前为止我认为它有很大的潜力。我特别喜欢这样一种方式,一旦你开始考虑地点的应用程序,浏览器历史几乎没有任何额外的努力就会落在你的腿上。然而,

然而, ,但有一件事情让我困扰:迄今为止,我所见过的所有文章和代码示例都只有一个(就我所关注的主要方面而言):'MVP'的'M'部分,即模型! / p>

在正常的MVP体系结构中,据我所知,Presenter拥有对Model的引用,并负责根据UI事件更新它,或者分别更新根据模型的变化,用户界面。



现在,在P& A的所有文章/样本中,Activities似乎取代了Presenter, '普通'的演示者,每当有新的场所到达时,他们就会被丢弃并(重新)创建,所以他们不能存储客户端状态,或者每次都会丢失。活动创建起来很便宜,所以没有什么麻烦,但我不想一次又一次地创建复杂应用程序的模型。



所有样本非常简单,并没有太多的状态,因此只是忽略了模型方面,但实际的复杂应用程序将其状态存储在何处?

解决方案

我想我找到了自己的答案。主要问题似乎是所有简单的代码示例都使 成为演示者,如下所示:

  public class NavigationActivity extends AbstractActivity implements NavigationView.Presenter {
private final ClientFactory clientFactory;
私人决赛NavigationPlace地方;

public NavigationActivity(ClientFactory clientFactory,NavigationPlace place){
this.clientFactory = clientFactory;
this.place = place;
}

@Override
public void start(AcceptsOneWidget panel,EventBus eventBus){
NavigationView navView = clientFactory.getNavigationView();
navView.setSearchTerm(place.getSearchTerm());
navView.setPresenter(this);
panel.setWidget(navView);
}

@Override
public void goTo(Place place){
clientFactory.getPlaceController()。goTo(place);


$ / code $ / pre
$ b $现在活动相当短暂, 古典意义上的演示者具有更长的使用寿命,以维持模型和UI之间的绑定。所以我所做的就是使用标准的MVP设计模式来实现一个单独的Presenter,并且所有的Activity都是这样的:

  public class NavigationActivity extends AbstractActivity {
private final ClientFactory clientFactory;
私人决赛NavigationPlace地方;

public NavigationActivity(ClientFactory clientFactory,NavigationPlace place){
this.clientFactory = clientFactory;
this.place = place;

$ b @Override
public void start(AcceptsOneWidget panel,EventBus eventBus){
NavigationPresenter presenter = clientFactory.getNavigationPresenter();
presenter.setSearchTerm(place.getSearchTerm());
presenter.go(面板);




$ b所以,而不是Activity抓取查看,并像一个演示者一样,它获取实际的 Presenter,只是通知它由Place引发的客户端状态改变,并告诉它在哪里显示其信息(即风景)。然后,Presenter可以自由地以任何方式管理视图和模型 - 这种方式比我已经找到的代码示例更好地工作(至少我为我正在处理的应用程序所考虑的内容)很远。


I'm trying to familiarize myself with the "Places & Activities" design pattern for GWT development, and so far I think it has a lot of potential. I especially like the way how once you start thinking about your application in terms of "Places", browser history virtually just lands in your lap with almost no extra effort.

However, one thing just bothers me: All the articles and code examples I've seen so far gloss over one (as far as I am concerned, major) aspect: the 'M' part of the 'MVP', i.e. the Model!

In normal MVP architecture, as far as I understand it, the Presenter holds a reference to the Model, and is responsible for updating it according to UI events or, respectivly, updating the UI according to Model changes.

Now, in all the articles/samples for "P&A" the Activities seem to be taking the place of the Presenter, but unlike 'normal' Presenters, they get discarded and (re-)created whenever a new Place arrives, so they can't be the ones to store the client state, or it would be lost every time. Activities are cheap to create, so it's not much of a hassle, but I wouldn't want to create the model of a complex application over and over again.

All the samples are fairly simple and don't have much of a state and therefore just ignore the Model aspect, but where would an actual, complex application store its state?

解决方案

I think I found my own answer. The main problem seems to be that all of the simple code examples make the Activities be the Presenters, as in:

public class NavigationActivity extends AbstractActivity implements NavigationView.Presenter {
    private final ClientFactory clientFactory;
    private final NavigationPlace place;

    public NavigationActivity(ClientFactory clientFactory, NavigationPlace place) {
        this.clientFactory = clientFactory;
        this.place = place;
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        NavigationView navView = clientFactory.getNavigationView();
        navView.setSearchTerm(place.getSearchTerm());
        navView.setPresenter(this);
        panel.setWidget(navView);
    }

    @Override
    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }
}

Now Activities are fairly short-lived, whereas a typical Presenter in the 'classical' sense has a much longer lifespan in order to maintain the binding between the model and the UI. So what I did was to implement a separate Presenter using the standard MVP design pattern, and all the Activity does is something like this:

public class NavigationActivity extends AbstractActivity {
    private final ClientFactory clientFactory;
    private final NavigationPlace place;

    public NavigationActivity(ClientFactory clientFactory, NavigationPlace place) {
        this.clientFactory = clientFactory;
        this.place = place;
    }

    @Override
    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        NavigationPresenter presenter = clientFactory.getNavigationPresenter();
        presenter.setSearchTerm(place.getSearchTerm());
        presenter.go(panel);
    }
}

So, instead of the Activity fetching the View, and acting like a Presenter on it, it fetches the actual Presenter, just informs it of the client state change induced by the Place, and tells it where to display its information (i.e. the view). And the Presenter is then free to manage the view and the model any way it likes - this way works a lot better (at least with what I have in mind for the application I'm working on) than the code examples I have found so far.

这篇关于GWT MVP与Places&活动 - 模型在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆