我正在使用Model-View-Presenter设计模式和EventBus(Otto)。我实现此模式的全部原因是仅将事件与演示者分离,并让演示者更新视图。
这是我所拥有的一些代码的示例,我将使用获取Events
作为示例。 (请注意,Events
与EventBus
Event
不同,这意味着Event
中的Events
是类似于"爸爸的事件生日",但Event
中的EventBus
是公交活动。
片段
public class EventFragment extends Fragment {
private EventPresenter mEventPresenter;
// Initialize boilerplate code...
private void init() {
mEventPresenter = new EventPresenter();
mEventPresenter.loadEvents();
}
// I WANT TO MOVE THESE SUBSCRIPTION METHODS TO
// MY PRESENTER OR SUBSCRIBER, BUT THEY ARE
// COUPLED TO THE ADAPTER OR A VIEW
@Subscribe
public void onEventsLoaded(EventsLoaded eventsLoaded) {
List<Event> events = eventsLoaded.getEvents();
mAdapter.setEvents(events);
}
@Subscribe
public void onEventJoined(EventJoined eventJoined) {
mItemView.doAnimation();
mTextView.setText("Leave");
mAdapter.joinEvent(eventJoined.getEvent());
}
@Subscribe
public void onLeaveEvent(LeftEvent leftEvent) {
mItemView.doAnimation();
mTextView.setText("Join");
mAdapter.leftEvent(leftEvent.getEvent());
}
}
演示
public class EventPresenter {
// This is the only method I have right now kind of defeats the purpose of
// having a presenter
public void loadEvents() {
EventBus.getInstance().post(new LoadEvents());
}
}
订户
public class EventSubscriber extends Subscriber {
// This class is registered on the event bus
@Subscribe
public void onLoadEvents(LoadEvents loadEvents) {
sClient.getEvents(new Callback<List<Event>>() {
@Override
public void onSuccess(List<Event> events, Response response) {
EventBus.post(new EventsLoaded(events));
}
@Override
public void onFailure(.....) {
// Handle failure
}
};
}
}
如何让Presenters和Subscribers处理所有业务逻辑,让Fragment只处理视图?
答案 0 :(得分:5)
这是主持人发送或接收事件以及通知片段或活动后的责任
答案 1 :(得分:5)
这不是最好的可能,但可以帮助你。
在演示者中订阅事件,其中包含一个视图实例可以提供帮助。就像presenter从eventbus获取事件并调用适当的视图方法来更新UI。
请注意,我没有更改更新UI的片段的方法名称,并且直接从演示者传递了事件对象,演示者现在确实是eventbus事件的订阅者。你可以相应地改变它。
片段---
public class EventFragment extends Fragment {
private EventPresenter mEventPresenter;
// Initialize boilerplate code...
@Override
public void onResume()
{
mEventPresenter.onResume();
}
@Override
public void onPause()
{
mEventPresenter.onPause();
}
private void init() {
mEventPresenter = new EventPresenter(this);
mEventPresenter.loadEvents();
}
// I WANT TO MOVE THESE SUBSCRIPTION METHODS TO
// MY PRESENTER OR SUBSCRIBER, BUT THEY ARE
// COUPLED TO THE ADAPTER OR A VIEW
public void onEventsLoaded(EventsLoaded eventsLoaded) {
List<Event> events = eventsLoaded.getEvents();
mAdapter.setEvents(events);
}
public void onEventJoined(EventJoined eventJoined) {
mItemView.doAnimation();
mTextView.setText("Leave");
mAdapter.joinEvent(eventJoined.getEvent());
}
public void onLeaveEvent(LeftEvent leftEvent) {
mItemView.doAnimation();
mTextView.setText("Join");
mAdapter.leftEvent(leftEvent.getEvent());
}
}
演示----
public class EventPresenter {
private EventFragment targetView;
public EventPresenter(EventFragment myView)
{
targetView = myView;
}
// This is the only method I have right now kind of defeats the purpose of
// having a presenter
public void loadEvents() {
EventBus.getInstance().post(new LoadEvents());
}
@Subscribe
public void onEventsLoaded(EventsLoaded eventsLoaded) {
targetView.onEventsLoaded(eventsLoaded);
}
@Subscribe
public void onEventJoined(EventJoined eventJoined) {
targetView.onEventJoined(eventJoined);
}
@Subscribe
public void onLeaveEvent(LeftEvent leftEvent) {
targetView.onLeaveEvent(leftEvent);
}
public void onResume()
{
//subscride event bus
}
public void onPause()
{
//unsubscride event bus
}
}