java如何在MVI模式中表示状态
让我们想象一个有新闻列表的屏幕,我可以用两种方式呈现状态
- 一个大目标
- 密封类,类
由于VMI需要对状态进行免疫,因此我们应该将UI状态更改为一个字段,而不会对状态进行部分更改,并且状态应随时保存所有UI数据李>
//案例1:一个大对象
class NewsViewState {
Throwable error; // if not null, an error has occurred
boolean loading; // if true loading data is in progress
List<News> result; // if not null this is the result of the search
}
//案例2:密封类,类
public interface SearchViewState {
/**
* Loading: Currently waiting for search result
*/
final class Loading implements SearchViewState {
}
}
案例一激活MVI目标,因为每次需要更改整个对象时,整个对象表示状态
正如您所看到的,在加载案例中,没有字段被称为结果或错误作为案例1,因此我的问题是为什么推荐使用案例2(密封类),尽管它违反了MVI原则
共 (0) 个答案