简体   繁体   English

使用MVP模式

[英]Using the MVP pattern

I have this web application which I have made with MVC pattern, however I am trying to adapt the MVP pattern. 我有这个用MVC模式制作的Web应用程序,但是我正在尝试调整MVP模式。

I am using the GWTPlatform library which I have migrated some of the codes already, mainly to the Presenter and the View . 我正在使用GWTPlatform库,我已经迁移了一些代码,主要是迁移到PresenterView However I have not seen any comprehensive material to explain how to actually deal with the Model part. 但是,我没有看到任何全面的材料来解释如何实际处理Model部分。 In my MVC pattern I have the model in the controller (in MVC) and the Views listen to changes in the Model to update the views. 在我的MVC模式中,我在控制器中有模型(在MVC中),并且视图监听模型中的更改以更新视图。 The model is updated by the controller, eg fireUpdateUser() function is fired as a result of opening the "user page" for example which then updates the model. 模型由控制器更新,例如fireUpdateUser()函数由于打开“用户页面”而被触发,例如然后更新模型。

How to I actually deal with the model in MVP if I already have remote services RPC (eg UserService, UserServiceImpl); 如果我已经有远程服务RPC(例如UserService,UserServiceImpl),我如何实际处理MVP中的模型; With Gwtplatform, I can just put a RPC call in the onReset() function of a presenter then essentially do a getView().getSomething().setValue(something) to update the View associated. 使用Gwtplatform,我可以在演示者的onReset()函数中放置一个RPC调用,然后基本上执行getView().getSomething().setValue(something)来更新View关联的。 In this case I did not have to use any model at all? 在这种情况下,我根本不需要使用任何模型? Also, what are the purposes of EventHandler and Activities? 此外,EventHandler和活动的目的是什么?

In your services you can inject DAO objects that deal with your data (model). 在您的服务中,您可以注入处理数据(模型)的DAO对象。 You usually have an interface and its implementation. 您通常有一个接口及其实现。

public interface IMyDao {
    List<String> getAllObject();
}

public class MyDao implements IMyDao {
    public List<String> getAllObject() {
        List<String> os = new ArrayList<String>();
        // DB access or Datastore (Sample code)
        os = datastore.query(...);
        return os;
    }
}

and in your service 并在您的服务

public class ServiceImpl implements Service {

  private final MyDao dao;

  @Inject
  public ServiceImpl(final MyDao dao) {
    this.dao = dao;
  }

  public List<String> getAllObject() {
    // Some processing
    return dao.getAllObject();
  }
}

Your service will be called by the presenter. 您的服务将由演示者调用。 So the workflow is Presenter -> Dao (Model) -> View (updated by the presenter). 所以工作流程是Presenter - > Dao(Model) - > View(由演示者更新)。

Have a look at that ebook , it will give you some ideas. 看看那本电子书 ,它会给你一些想法。

I suggest you read this articles, they describe the basic concepts GWTPlatform, and examples of using it: 我建议你阅读这篇文章,他们描述了GWTPlatform的基本概念,以及使用它的例子:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM