|
| 1 | +# TODO-MVP |
| 2 | + |
| 3 | +### Summary |
| 4 | + |
| 5 | +This sample is the base for many of the variants. It showcases a simple |
| 6 | +implementation of the Model-View-Presenter pattern with no architectural |
| 7 | +frameworks. It uses manual dependency injection to provide a repository with |
| 8 | +local and remote data sources. Asynchronous tasks are handled with callbacks. |
| 9 | + |
| 10 | +<img src="https://github.com/googlesamples/android-architecture/wiki/images/mvp.png" alt="Diagram"/> |
| 11 | + |
| 12 | +Note: in a MVP context, the term "view" is overloaded: |
| 13 | + |
| 14 | + * The class android.view.View will be referred to as "Android View" |
| 15 | + * The view that receives commands from a presenter in MVP, will be simply called |
| 16 | +"view". |
| 17 | + |
| 18 | +### Fragments |
| 19 | + |
| 20 | +It uses fragments for two reasons: |
| 21 | + |
| 22 | + * The separation between Activity and Fragment fits nicely with this |
| 23 | +implementation of MVP: the Activity is the overall controller that creates and |
| 24 | +connects views and presenters. |
| 25 | + * Tablet layout or screens with multiple views take advantage of the Fragments |
| 26 | +framework. |
| 27 | + |
| 28 | +### Key concepts |
| 29 | + |
| 30 | +There are four features in the app: |
| 31 | + |
| 32 | + * <code>Tasks</code> |
| 33 | + * <code>TaskDetail</code> |
| 34 | + * <code>AddEditTask</code> |
| 35 | + * <code>Statistics</code> |
| 36 | + |
| 37 | +Each feature has: |
| 38 | + |
| 39 | + * A contract defining the view and the presenter |
| 40 | + * An Activity which is responsible for the creation of fragments and presenters |
| 41 | + * A Fragment which implements the view interface. |
| 42 | + * A presenter which implements the presenter interface |
| 43 | + |
| 44 | +In general, the business logic lives in the presenter and relies on the view to |
| 45 | +do the Android UI work. |
| 46 | + |
| 47 | +The view contains almost no logic: it converts the presenter's commands to UI |
| 48 | +actions and listens to user actions, which are passed to the presenter. |
| 49 | + |
| 50 | +Contracts are interfaces used to define the connection between views and |
| 51 | +presenters. |
| 52 | + |
| 53 | +### Dependencies |
| 54 | + |
| 55 | + * Common Android support libraries (<code>com.android.support.\*)</code> |
| 56 | + * Android Testing Support Library (Espresso, AndroidJUnitRunner…) |
| 57 | + * Mockito |
| 58 | + * Guava (null checking) |
| 59 | + |
| 60 | +## Features |
| 61 | + |
| 62 | +### Complexity - understandability |
| 63 | + |
| 64 | +#### Use of architectural frameworks/libraries/tools: |
| 65 | + |
| 66 | +None |
| 67 | + |
| 68 | +#### Conceptual complexity |
| 69 | + |
| 70 | +Low, as it's a pure MVP implementation for Android |
| 71 | + |
| 72 | +### Testability |
| 73 | + |
| 74 | +#### Unit testing |
| 75 | + |
| 76 | +High, presenters are unit tested as well as repositories and data sources. |
| 77 | + |
| 78 | +#### UI testing |
| 79 | + |
| 80 | +High, injection of fake modules allow for testing with fake data |
| 81 | + |
| 82 | +### Code metrics |
| 83 | + |
| 84 | +Compared to a traditional project with no architecture in place, this sample |
| 85 | +introduces additional classes and interfaces: presenters, a repository, |
| 86 | +contracts, etc. So lines of code and number of classes are higher in MVP. |
| 87 | + |
| 88 | + |
| 89 | +``` |
| 90 | +------------------------------------------------------------------------------- |
| 91 | +Language files blank comment code |
| 92 | +------------------------------------------------------------------------------- |
| 93 | +Java 46 1075 1451 3451 |
| 94 | +XML 34 97 337 601 |
| 95 | +------------------------------------------------------------------------------- |
| 96 | +SUM: 80 1172 1788 4052 |
| 97 | +------------------------------------------------------------------------------- |
| 98 | +``` |
| 99 | +### Maintainability |
| 100 | + |
| 101 | +#### Ease of amending or adding a feature |
| 102 | + |
| 103 | +High. |
| 104 | + |
| 105 | +#### Learning cost |
| 106 | + |
| 107 | +Low. Features are easy to find and the responsibilities are clear. Developers |
| 108 | +don't need to be familiar with any external dependency to work on the project. |
| 109 | + |
0 commit comments