The place where random ideas get written down and lost in time.

2019-06-28 - The MCVP pattern

Category DEV

A while ago I wanted to take a consistent approach at splitting my projects into a “UI vs core” pattern. My old C# skeleton generated 2 libs upfront for that. In other android apps, I have either an android lib or at least clear packages “core vs ui”.

Looking at something like Flutter, the benefits would be useful if the app and UI is using one framework and then the “core” of the app uses another framework, namely a POJO or at least an Android lib. In the case of a game, the latter could be a Unity library.

When doing a pure Android app of a limited scope, there’s little value in that separation. It becomes more useful when I try alternate frameworks, for example Flutter or Kotlin. The idea is that if I write the UI part in Flutter now and the framework falls in desuetude, I could rewrite just the UI later.

One thing I noticed is that if I do not force myself to do the split upfront using actual libraries, it will not happen easily after. Forcing to have an android app + an android lib imposes some constraints in the import/dependency structure. It forces us to make callbacks instead of direct bi-directional calls. It forces us to make the boundaries clear. Dagger would help a bit to ease using one with the other.

As far as patterns go, there’s MVP and MVC. I suggest a mix, which I’m calling MCVP:

  • Model: Model data/classes, in the core feature library.
  • Controllers: Action classes acting solely on the model, in the core feature library.
  • Views: Pure UI elements display model, in the app/ui library.
  • Presenters: Action classes managing the interaction, in the app/ui library.

The app logic would be both in controllers and presenters. The controllers are purely model-oriented and by having them in the core lib, they have no impact on the UI. The presenters on the other hand coordinate between the views, model, and controllers. They are the “business logic” that is located at the app/ui level.

As a graph, we would have:

[model] ⇐⇒ [controllers] ⇐⇒ [presenters] ⇐⇒ [views]


 Generated on 2024-12-02 by Rig4j 0.1-Exp-f2c0035