Using the MVP pattern

女生的网名这么多〃 提交于 2019-12-10 15:28:25

问题


I have this web application which I have made with MVC pattern, however I am trying to adapt the MVP pattern.

I am using the GWTPlatform library which I have migrated some of the codes already, mainly to the Presenter and the View. However I have not seen any comprehensive material to explain how to actually deal with the Model part. 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. The model is updated by the controller, e.g fireUpdateUser() function is fired as a result of opening the "user page" for example which then updates the model.

How to I actually deal with the model in MVP if I already have remote services RPC (e.g UserService, UserServiceImpl); 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. In this case I did not have to use any model at all? Also, what are the purposes of EventHandler and Activities?


回答1:


In your services you can inject DAO objects that deal with your data (model). 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).

Have a look at that ebook, it will give you some ideas.




回答2:


I suggest you read this articles, they describe the basic concepts GWTPlatform, and examples of using it:

  • GWTPlatform Getting Started
  • Other getting Started
  • Blog about GWTP


来源:https://stackoverflow.com/questions/10155289/using-the-mvp-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!