Search code examples
javagwtmvpgwt-platform

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.

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?


Solution

  • 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.