|
| 1 | +--- |
| 2 | +title: Model-View-Controller |
| 3 | +category: Architectural |
| 4 | +language: en |
| 5 | +tags: |
| 6 | + - Decoupling |
| 7 | + - Encapsulation |
| 8 | +--- |
| 9 | + |
| 10 | +## Intent |
| 11 | +MVI is a derivation of the original MVC architectural pattern. Instead of working with a |
| 12 | +proactive controller MVI works with the reactive component called intent: it's a component |
| 13 | +which translates user input events into model updates. |
| 14 | + |
| 15 | +## Explanation |
| 16 | + |
| 17 | +> MVI is a Reactive Architecture Pattern which is short for Model -View-Intent. |
| 18 | +It introduces two new concepts: the intent and the state. |
| 19 | +UI might have different states — Loading State, Fetch Data State, Error State, |
| 20 | +and user events are submitted in the form of an Intent. |
| 21 | + |
| 22 | +* [Stateful Android Apps With MVI (MODEL — VIEW — INTENT)](https://medium.com/huawei-developers/stateful-android-apps-with-mvi-architecture-model-view-intent-d106b09bd967) |
| 23 | + |
| 24 | +## Class diagram |
| 25 | + |
| 26 | + |
| 27 | +**Programmatic Example** |
| 28 | + |
| 29 | +CalculatorAction defines our Intent in MVI for user interactions. It has to be an interface |
| 30 | +instead of enum, so that we can pass parameters to certain children. |
| 31 | +```java |
| 32 | +public interface CalculatorAction { |
| 33 | + |
| 34 | + /** |
| 35 | + * Makes identifying action trivial. |
| 36 | + * |
| 37 | + * @return subclass tag. |
| 38 | + * */ |
| 39 | + String tag(); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +CalculatorModel defines the state of our view or in out case, variable and output of the calculator. |
| 44 | +```java |
| 45 | +@Data |
| 46 | +public class CalculatorModel { |
| 47 | + |
| 48 | + /** |
| 49 | + * Current calculator variable used for operations. |
| 50 | + **/ |
| 51 | + final Double variable; |
| 52 | + |
| 53 | + /** |
| 54 | + * Current calculator output -> is affected by operations. |
| 55 | + **/ |
| 56 | + final Double output; |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | + |
| 61 | +CalculatorView will serve as a mock view which will expose potential user actions and |
| 62 | +display calculator state -> output and current variable |
| 63 | +```java |
| 64 | +@Slf4j |
| 65 | +public class CalculatorView { |
| 66 | + |
| 67 | + /** |
| 68 | + * View model param handling the operations. |
| 69 | + * */ |
| 70 | + private final CalculatorViewModel viewModel = new CalculatorViewModel(); |
| 71 | + |
| 72 | + /** |
| 73 | + * Display current view model output with logger. |
| 74 | + * */ |
| 75 | + void displayTotal() { |
| 76 | + LOGGER.info( |
| 77 | + "Total value = {}", |
| 78 | + viewModel.getCalculatorModel().getOutput().toString() |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Handle addition action. |
| 84 | + * */ |
| 85 | + void add() { |
| 86 | + viewModel.handleAction(new AdditionCalculatorAction()); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Handle subtraction action. |
| 91 | + * */ |
| 92 | + void subtract() { |
| 93 | + viewModel.handleAction(new SubtractionCalculatorAction()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Handle multiplication action. |
| 98 | + * */ |
| 99 | + void multiply() { |
| 100 | + viewModel.handleAction(new MultiplicationCalculatorAction()); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Handle division action. |
| 105 | + * */ |
| 106 | + void divide() { |
| 107 | + viewModel.handleAction(new DivisionCalculatorAction()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Handle setting new variable action. |
| 112 | + * |
| 113 | + * @param value -> new calculator variable. |
| 114 | + * */ |
| 115 | + void setVariable(final Double value) { |
| 116 | + viewModel.handleAction(new SetVariableCalculatorAction(value)); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +Finally, ViewModel handles the exposed events with the handleAction(event) method, which delegates |
| 122 | +the specific handling to private methods. Initially calculator output and variable are equal to 0. |
| 123 | +```java |
| 124 | +public final class CalculatorViewModel { |
| 125 | + |
| 126 | + /** |
| 127 | + * Current calculator model (can be changed). |
| 128 | + */ |
| 129 | + private CalculatorModel model = |
| 130 | + new CalculatorModel(0.0, 0.0); |
| 131 | + |
| 132 | + /** |
| 133 | + * Handle calculator action. |
| 134 | + * |
| 135 | + * @param action -> transforms calculator model. |
| 136 | + */ |
| 137 | + void handleAction(final CalculatorAction action) { |
| 138 | + switch (action.tag()) { |
| 139 | + case AdditionCalculatorAction.TAG -> add(); |
| 140 | + case SubtractionCalculatorAction.TAG -> subtract(); |
| 141 | + case MultiplicationCalculatorAction.TAG -> multiply(); |
| 142 | + case DivisionCalculatorAction.TAG -> divide(); |
| 143 | + case SetVariableCalculatorAction.TAG -> { |
| 144 | + SetVariableCalculatorAction setVariableAction = |
| 145 | + (SetVariableCalculatorAction) action; |
| 146 | + setVariable(setVariableAction.getVariable()); |
| 147 | + } |
| 148 | + default -> { |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Getter. |
| 155 | + * |
| 156 | + * @return current calculator model. |
| 157 | + */ |
| 158 | + public CalculatorModel getCalculatorModel() { |
| 159 | + return model; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Set new calculator model variable. |
| 164 | + * |
| 165 | + * @param variable -> value of new calculator model variable. |
| 166 | + */ |
| 167 | + private void setVariable(final Double variable) { |
| 168 | + model = new CalculatorModel( |
| 169 | + variable, |
| 170 | + model.getOutput() |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Add variable to model output. |
| 176 | + */ |
| 177 | + private void add() { |
| 178 | + model = new CalculatorModel( |
| 179 | + model.getVariable(), |
| 180 | + model.getOutput() + model.getVariable() |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Subtract variable from model output. |
| 186 | + */ |
| 187 | + private void subtract() { |
| 188 | + model = new CalculatorModel( |
| 189 | + model.getVariable(), |
| 190 | + model.getOutput() - model.getVariable() |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * Multiply model output by variable. |
| 196 | + */ |
| 197 | + private void multiply() { |
| 198 | + model = new CalculatorModel( |
| 199 | + model.getVariable(), |
| 200 | + model.getOutput() * model.getVariable() |
| 201 | + ); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Divide model output by variable. |
| 206 | + */ |
| 207 | + private void divide() { |
| 208 | + model = new CalculatorModel( |
| 209 | + model.getVariable(), |
| 210 | + model.getOutput() / model.getVariable() |
| 211 | + ); |
| 212 | + } |
| 213 | +} |
| 214 | +``` |
| 215 | + |
| 216 | +## Applicability |
| 217 | +Use the Model-View-Intent pattern when |
| 218 | + |
| 219 | +* You want to clearly separate the domain data from its user interface representation |
| 220 | +* You want to minimise the public api of the view model |
| 221 | + |
| 222 | +## Known uses |
| 223 | +A popular architecture pattern in android. The small public api is particularly powerful |
| 224 | +with the new Android Compose UI, as you can pass a single method (viewModel::handleEvent) |
| 225 | +to all Composables(parts of UI) as a callback for user input event. |
| 226 | + |
| 227 | +## Consequences |
| 228 | +Pros: |
| 229 | +* Encapsulation |
| 230 | +* Separation of concerns |
| 231 | +* Clear list of all possible user events |
| 232 | + |
| 233 | +Cons: |
| 234 | +* More boilerplate code compared to alternatives (especially in Java) |
| 235 | + |
| 236 | +## Related patterns |
| 237 | +MVC: |
| 238 | +* [Trygve Reenskaug - Model-view-controller](http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) |
| 239 | +* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=48d37c67fb3d845b802fa9b619ad8f31) |
| 240 | + |
| 241 | +## Credits |
| 242 | + |
| 243 | +* [Model View Intent: a new Android Architecture Pattern](https://apiumacademy.com/blog/model-view-intent-pattern/) |
| 244 | +* [MVI Architecture for Android Tutorial](https://www.kodeco.com/817602-mvi-architecture-for-android-tutorial-getting-started) |
| 245 | + |
0 commit comments