Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any best practice for packaging MVP layers?

Tags:

java

android

mvp

I have done some Android applications using MVP methodology,

But I am not so sure if it is better to place different layers objects of the same feature in a same package? or package all layers items of different features in the same package with layer name on it?

(Which I mean something like this)

Packages Screenshot

Currently, I am following the second rule, but is this the best practice?

Edit: This is my project's whole packages! :)

Packages bigger screenshot

like image 487
Mohsen Mirhoseini Avatar asked Oct 15 '16 07:10

Mohsen Mirhoseini


2 Answers

Just to throw my thoughts into the mix. I have worked on projects with each of these approaches. My preference now is to package by feature. There are two main reasons I prefer this approach:

Ease of Induction

Increased visibility of project structure for developers new to the codebase. When classes which relate to a single feature are grouped together it is much easier for a new team member to quickly see how everything fits together.

Restricting Class access

This is probably more important. When you package by type (all Presenters together, etc) you have to give many methods in these classes public access. This can lead to these functions being used inappropriately from various areas of the codebase.

If you package by feature then these classes are all in the same package and as such you can give the methods package level access. This ensures that methods don't leak.

like image 154
Jahnold Avatar answered Nov 12 '22 20:11

Jahnold


Create a feature-specific package which contains a Contract interface containing the View and Presenter interfaces. The feature package also contains implementations of the View and Presenter interfaces (i.e. the Activity and Presenter classes).

I have made a sample project here; you may refer it for more info on MVP.

like image 2
Daksh Avatar answered Nov 12 '22 21:11

Daksh