jetpack_关于Jetpack的初步构想

jetpack

设计模式(Design Patterns)

There was one book, which made me understand a big problem with the Android View System. The book is called Design Patterns and is written by the infamous gang of four.

有一本书,这使我了解了Android View System的一个大问题。 这本书叫做《设计模式》 ,由四人组成的臭名昭著的帮派撰写。

In the initial chapters, the authors take you along the development process of a graphical editor based on a view system. While following the process I became fascinated by how relevant this old book (1995) still seemed and how much I could actually apply to my coding style. There was one pattern that I found especially fitting for creating a view system, the Composite pattern. And it got me thinking that the Android view system seemed to ignore this pattern, besides this book being around for so long already. The Android code design is heavily based on inheritance with very large base classes, which is for me today one of the main reasons the code became hard to maintain in the long run. Besides the fact of course that its really really old.

在最初的章节中,作者将带您了解基于视图系统的图形编辑器的开发过程。 在遵循这一过程的同时,我着迷于这本旧书(1995)仍然显得多么相关,以及我实际上可以将多少应用于我的编码风格。 我发现有一种模式特别适合创建视图系统,即Composite模式。 让我想到,除了本书已经存在了这么长时间之外,Android视图系统似乎忽略了这种模式。 Android代码设计在很大程度上基于具有非常大的基类的继承,这对我而言,这是从长远来看代码变得难以维护的主要原因之一。 当然,事实是它真的很老。

扑! (Flutter!)

Then Flutter came along and I was amazed. My first thought was that they must have read my beloved design patterns book! I started learning Flutter and the coding was as smooth as imagined. I even started to look for flutter projects and considered switching from Android. But in the end, my love for Kotlin didn’t make me rush on this move.

然后Flutter出现了,我很惊讶。 我首先想到的是,他们一定已经读过我心爱的设计模式书! 我开始学习Flutter,编码像想象中一样顺利。 我什至开始寻找开发项目,并考虑从Android切换。 但是最后,我对Kotlin的爱并没有使我急于采取这一行动。

模型视图意图 (Model View Intent)

Also, I just learned coroutines and this newish MVI pattern and I very much enjoyed this way of coding. MVI again remembered me of something I read in another one of those old books — Refactoring by Martin Fowler. Precisely the chapter duplicate observed data, which says if data is used in two layers, the upper layer should control the data and the lower layer should observe it. A very familiar example is the Jetpack Viewmodel controlling a state, which is observed by the Android view layer.

另外,我刚刚学习了协程和这种新的MVI模式,我非常喜欢这种编码方式。 MVI再次使我想起了我在另一本旧书中读到的东西-Martin Fowler的重构。 恰好本章重复观察到的数据,即如果在两层中使用数据,则上层应控制数据,而下层则应观察数据。 一个非常熟悉的示例是Jetpack Viewmodel控制状态,这由Android视图层观察到。

The advantage of this pattern is that there is one single source of truth and there is no syncing effort between the layers. In my opinion, this is a very important aspect to MVI, but unfortunately the Android view system is not really made for this, as the views in Android have state baked into them.

这种模式的优点是只有一个单一的事实来源,并且各层之间没有同步工作。 在我看来,这对于MVI来说是非常重要的方面,但是不幸的是,Android视图系统并不是为此真正打造的,因为Android中的视图已经融入其中。

Jetpack撰写 (Jetpack Compose)

This is the second reason why I'm very very excited about Jetpack Compose. It does not only solve those issues of the Android view system but does so in a very elegant way. Just check out how the animate function works and you will see what I mean 🤯

这是我对Jetpack Compose感到非常兴奋的第二个原因。 它不仅解决了Android视图系统的那些问题,而且以一种非常优雅的方式解决了这一问题。 只需检查动画功能如何工作,您就会明白我的意思🤯

But when I initially saw that there was a state inside the composables I was wondering, why the state is not just passed down the hierarchy and controlled by the ViewModel. Luckily there some good official explanations available.

但是,当我最初看到可组合对象内部存在一种状态时,我想知道为什么该状态不仅仅沿层次结构传递并由ViewModel控制。 幸运的是,这里有一些很好的官方解释。

You can think of internal state as a private variable in a class. The source of truth belongs to whoever creates and controls that state

您可以将内部状态视为类中的私有变量。 真理之源属于创造和控制那种状态的人

coming from the OOP world this was quiet plausible for me. A variable should be in the class, which is using it the most (in this case exclusively), also described in Martin Fowler's book as move field refactoring. A good example of a private state of a composable would be the scroll state of a scrollable element. On the other hand, if the state is actually needed by multiple composables you should lift the state up in a higher layer, this is what they refer in the documentation to as state hoisting. There are quite some other aspects to how state is handled in composables like ambient, but that's something still left to explore and to write down in another blog post fully dedicated to state handling.Thanks for reading and to share some more thoughts on Jetpack Compose lets connect on Twitter.

来自OOP世界,这对我来说似乎很安静。 变量应该在该类中使用最多(在本例中为唯一),该类在Martin Fowler的书中也描述为移动字段重构。 可组合元素的私有状态的一个很好的例子是可滚动元素的滚动状态。 另一方面,如果多个可组合物实际上需要该状态,则应将状态提升到更高的层次,这就是它们在文档中称为状态提升。 在诸如环境这样的可组合对象中如何处理状态还有很多其他方面但这仍然需要探索并写下另一篇专门致力于状态处理的博客文章中。感谢阅读和分享有关Jetpack Compose的更多想法在Twitter上连接。

翻译自: https://medium.com/@adrianocelentano/first-thoughts-on-jetpack-compose-f95645684455

jetpack

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值