Flutter architectural pattern — VMS
View Mutator State (VMS).
Why did I decide to develop my own solution in architectural patterns?
The answer is simple! After a lot of reading about different architectures in the net, discussing in chats and so on, I found a thing, that a lot of architectures contains a lot of entities. People creating layers and layers, layers and layers…
We all know SOLID and OOP, probably you know Yegor Bugaenko — the Jedi of OOP. Good OOP means that if one property changing in our object, we should destroy it and create a new one. Well, sometimes we must have huge POJO classes, it doesn’t mean we can refactor it, no, we just should have a solid structure.
Here is the thing — we have a big object, user holder for example. In one moment user changing his own name, that happened in true oop style? Just bullshit! Why I should recreate all the data, spend a lot of computer resources? I don’t want to! My idea is — imagine that apple is the object and I am not creating a new one when I bite the apple. Apple still same, it is just bitten. Let’s call it mutation( changing structure ). To be more close to a programming language, let’s call the structure a state. And in the end, let’s describe a visual layer a view. What we have now?
More complex example:
I am using flutter and dart to create mobile applications, and you can notice that we can safely create instances of the classes just in the file. I will describe the logic when we create objects and don’t throw it to the trash and building new ones. We changing its properties by the mutator.
You probably noticed streams and streamBuilders. Don’t panic. Here are my description and source code: « Stream builder just listening to a stream with values, returning view based on information that we put in the stream. Mutator changes the properties of our object and puts new information in the source stream. Streambuilder listening to its values and react, when a new one comes in by returning new widget.»
Simple explanation : Wrap your views in StreamBuilder and put inside it datasource (stream).
API docs for the StreamBuilder class from the widgets library, for the Dart programming language.docs.flutter.io
If you need to get information from the database or Net, you can create a provider class, which contains async functions. Also, you can put this functionality into mutator class, but I afraid it will be bigger then must be.
What we have in the end? Immutable widgets — it is good, changing resources by not recreating objects — awesome, logic and views a separated. Do you need more to forget bloc or redux?) If you have interesting thoughts about my article let me know.