我有一位演示者P
。它旨在控制某种类型的视图V
的所有内容。我有多个这样的观看:V1
,V2
,... Vn
。与一个视图Vx
进行交互可能会对其他视图Vy
产生影响。
协调这个的最佳方法是什么?主持人应该互相交谈吗?主持人是否应该参考所有观点?
答案 0 :(得分:4)
One View应该对其他视图一无所知。 View应仅与其Presenter通信。一位主持人也不应该了解其他演示者。为了避免主持人凝聚使用事件(EventBus,Otto,RxJava主题......)。
答案 1 :(得分:1)
一种方法是主持人P
持有观看V
并观看V
继承V1
,V2
,... Vn
。所有View容器(片段和活动)中的V
都可以访问方法。
public interface MainView extends BaseView, ErrorView{ //V
void showProducts(@Nullable List<Product> products);
}
public interface BaseView{ //V1
void setLoading(boolean loading);
void showConfirmationDialog(@StringRes int title, @StringRes int message);
}
public interface ErrorView{ //V2
void showErrorSnackBar(@StringRes int message);
}
public class ProductListFragment extends ... implements MainView{
}
根据我的经验,除了杂乱和空洞的方法之外,这种方法很好,但在可读性方面并不好。
只要您确保他们还活着,就可以在主要演示者中保留其他演示者的引用。这可能会导致反模式。
另一种方法是分别保存每个演示者,每个演示者都独立使用事件:
@Override
public void showProducts(List<Product> products){
// do something with products that Presenter1 has dispatched for presenting
presenter2.doSomethingOnProducts();
}
这是结果中感兴趣的各方之间的无缝沟通。
将Observer
模式与MVP混合也很好。