以前我们所谓的MVC架构通常我们会在Model跟View直接交互,而MVP中我们采用Presenter作为中间Model获得数据后传给Presenter然后Presenter将结果传递给View显示。
说明:
步骤1:UI实现View方法,引用Presenter
步骤2:Presenter调用Model,走Model具体逻辑
步骤3:Model逻辑实现,回调Presenter方法
步骤4:Presenter回调View,即回到UI,回调View方法
所以基于上面的结构我们采用RxJava来实现Model的逻辑实现,整体的代码结构:
View层结构:
/**
* 定义一个View层表现
* Created by jayuchou on 16/2/25.
*/
public interface PersonView {
void startLoad();
void loadDataSuccess(Person person);
void loadDataFailed();
}
我们将最终界面有交互的功能都封装成一个View接口以供Presenter回调使用。
Presenter层结构:
我们先定义一个IPresenter接口,供View绑定和解绑使用
public interface IPersonPresenter<V> {
void attachView(V view);
void detachView();
}
然后我们定义一个PersonPresenter层作为View和Model的中间
public class PersonPresenter implements IPersonPresenter<PersonView> {
private PersonView mPersonView;
public PersonPresenter(PersonView mPersonView) {
attachView(mPersonView);
}
@Override
public void attachView(PersonView view) {
mPersonView = view;
}
@Override
public void detachView() {
mPersonView = null;
}
}
然后Model中的RxJava业务逻辑实现的方法如下:
public Subscription getPersonInfo() {
return Observable.create(new Observable.OnSubscribe<Person>() {
@Override
public void call(Subscriber<? super Person> subscriber) {
Person p = new Person();
p.age = "23";
p.name = "Jayuchou";
subscriber.onStart();
subscriber.onNext(p);
subscriber.onCompleted();
}
}).subscribe(new Subscriber<Person>() {
@Override
public void onStart() {
super.onStart();
mPersonView.startLoad();
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
mPersonView.loadDataFailed();
}
@Override
public void onNext(Person person) {
mPersonView.loadDataSuccess(person);
}
});
这里返回一个Subscription供界面解除对Observable的观察监听,但是我们如果把RxJava这段实现独立在Presenter层实现的话需要再把PersonPresenter作为参数传入,这样代码看起来比较啰嗦 所以我把model实现写在PersonPresenter类中,最终的PersonPresenter的代码完整版为:
/**
* P层 将数据从Model中传递到View
* Created by jayuchou on 16/2/25.
*/
public class PersonPresenter implements IPersonPresenter<PersonView> {
private PersonView mPersonView;
public PersonPresenter(PersonView mPersonView) {
attachView(mPersonView);
}
@Override
public void attachView(PersonView view) {
mPersonView = view;
}
@Override
public void detachView() {
mPersonView = null;
}
public Subscription getPersonInfo() {
return Observable.create(new Observable.OnSubscribe<Person>() {
@Override
public void call(Subscriber<? super Person> subscriber) {
Person p = new Person();
p.age = "23";
p.name = "Jayuchou";
subscriber.onStart();
subscriber.onNext(p);
subscriber.onCompleted();
}
}).subscribe(new Subscriber<Person>() {
@Override
public void onStart() {
super.onStart();
mPersonView.startLoad();
}
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
mPersonView.loadDataFailed();
}
@Override
public void onNext(Person person) {
mPersonView.loadDataSuccess(person);
}
});
}
}
最后在代码中调用我们需要先把我们的Activity/Fragment实现PersonView
public class MainActivity extends AppCompatActivity implements PersonView {
private Subscription mSubscription;
private PersonPresenter presenter;
@Override
public void startLoad() {
Log.i("Jayuchou", "-- startLoad...");
}
@Override
public void loadDataSuccess(Person person) {
Log.i("Jayuchou", "Success = " + person.toString());
}
@Override
public void loadDataFailed() {
Log.i("Jayuchou", "Load failed...");
}
}
然后调用Presenter中的方法即可了:
presenter = new PersonPresenter(MainActivity.this);
mSubscription = presenter.getPersonInfo();
这样我们一个MVP实现就完成了,虽然不是很严谨的按MVP设计过来,但是通过小Demo可以对MVP设计懂的更多了。