-1

I am trying to understand MVP design pattern practically and went through this link and few other links and made some observation. I want to know that all the below observations are correct for implementing MVP design pattern practically?

  1. Activity, Fragments and our xml layouts will be part of View.

  2. Our POJO classes or the classes which are responsible for fetching data, making API calls or calling Web services are part of Model.

  3. We create an interface which contains abstract methods for various events we need to perform on View or various events for the lifecycle of view. Activity/Fragment will implement that interface and pass its reference to Presenter constructor.

  4. Presenter will have reference to both View and Model. Its constructor will contain reference to an interface which Activity implemented and it will create an object of Model.

  5. Whenever an action is performed on View or for any lifecycle callback of View, a method of Presenter is called from View. That method will interact with both Model and View as per requirement. It will call method of Model and will call the method of interface that Activity implemented so both Model and View can perform action in their classes.

Onik
  • 19,396
  • 14
  • 68
  • 91
varmashrivastava
  • 432
  • 2
  • 6
  • 21

1 Answers1

1

Your understanding is mostly correct:

  1. Correct.
  2. Quite correct. Although note that in MVP design pattern terminology the notion Model (M) is pretty general. In practice Model is divided in a few layers depending on their "functionality", e.g. Interactor, Repository, network etc.
  3. In general, correct.
  4. Correct regarding the VP part and incorrect on "it will create an object of Model". Presenter should not create an instance of Model, it should communicate with it via an interface too.
  5. In general, correct. However, View should not care about lifecycle of View. Model should provide data.
Onik
  • 19,396
  • 14
  • 68
  • 91
  • Do you mean that model should implement an interface and presenter should have reference to it?Can you show a simple demo code how presenter and model communicate using interface assuming model as [this](https://github.com/ericmaxwell2003/ticTacToe/blob/mvp/app/src/main/java/com/acme/tictactoe/model/Board.java) and presenter as [this](https://github.com/ericmaxwell2003/ticTacToe/blob/mvp/app/src/main/java/com/acme/tictactoe/presenter/TicTacToePresenter.java) – varmashrivastava May 18 '18 at 05:27
  • @armashrivastava, correct! Here is a [good and simple example](https://antonioleiva.com/mvp-android/). It refers to a GitHub page where `FindItemsInteractor` is interface of Model. – Onik May 18 '18 at 08:14
  • FindItemsInteractorImpl is model..and activity is creating object for model through `new FindItemsInteractorImpl()`.Is this as per MVP? – varmashrivastava May 18 '18 at 08:19
  • @armashrivastava, yes. You have to create instances somewhere, right. You might use Dagger2, it will handle instances creation, but anyway you gonna have to instantiate Dagger Component in Activity. – Onik May 18 '18 at 08:24
  • But as per MVP, View and Model doesn't interact with each other but presenter and model can?So why not create instance of Model in Presenter only? – varmashrivastava May 18 '18 at 08:31
  • @armashrivastava, View does not interact with Model, Activity/Fragment just creates instances of Presenter and Model, because in Android those also play role of app entry points, not just role of a View. In a desktop Java app you would create those instances in the `main` method. Regarding Presenter, it's not an app entry point so it should not create instances of View or Model. – Onik May 18 '18 at 09:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171305/discussion-between-onik-and-varmashrivastava). – Onik May 18 '18 at 09:27
  • Actually, there's another practical reason why you want to create the model in the Activity/Fragment. As @Onik correctly pointed out, Android, the presenter is not the entry point.. the Activity or Fragment is.. they are also the owners of the lifecycle (awkward). Additionally, any parameters passed to the entry point exist in the Activity/Fragment and you probably want to pass those to the model on creation.. which is very awkward in the presenter. You might find my framework for MVP (https://github.com/cjsoftware-lib/ucsFramework) interesting. – C B J Jul 07 '18 at 07:55