其实我们日常开发中的Activity,Fragment和XML界面就相当于是一个 MVC 的架构模式,Activity中不仅要处理各种 UI 操作还要请求数据以及解析。这样的后果就是Activity可能会有几千行的代码,不便于维护,基于此现象及解耦的原则,才使用MVP的架构模式进行分离代码,其次MVP便于后期的代码重用,比如相同的业务逻辑,不同的ui界面等,开发MVP框架最重要的注意事项是内存泄漏。
在MVP中,M代表Model,V代表View,P代表Presenter。
Model 负责获取数据,数据的来源可以是网络或本地数据库等;
View 负责界面数据的展示,与用户进行交互;
Presenter 是Model与View之间的通信的桥梁,将Model与View分离开来。
首先开发P层
首先定义一个ILifeCircle来关联View层的生命周期的方法,ILifeCircle
定义成接口可以进行拓展。
public interface ILifeCircle {
void onCreate(Bundle savedInstanceState, Intent intent,Bundle getArguments);
void onActivityCreated(Bundle savedInstanceState, Intent intent,Bundle getArguments);
void onStart();
void onResume();
void onPause();
void onStop();
void onDestroy();
void destroyView();
void onViewDestroy();
void onNewIntent(Intent intent);
void onActivityResult(int requestCode, int resultCode, Intent data);
void onSaveInstanceState(Bundle bundle);
void attachView(IMvpView iMvpView);
}
LifeCircleMvpPresenter实现ILifeCircle接口,构造传递IMvpView,关联view并且把关联的View,使用代理类MvpControler实现生命周期的关联
public abstract class LifeCircleMvpPresenter implements ILifeCircle {
protected WeakReference weakReference;
protected LifeCircleMvpPresenter() {
super();
}
public LifeCircleMvpPresenter(IMvpView iMvpView) {
super();
attachView(iMvpView);
MvpControler mvpControler = iMvpView.getMvpControler();
mvpControler.savePresenter(this);
}
@Override
public void attachView(IMvpView iMvpView) {
if (weakReference == null) {
weakReference = new WeakReference(iMvpView);
} else {
T view = (T) weakReference.get();
if (view != iMvpView) {
weakReference = new WeakReference(iMvpView);
}
}
}
@Override
public void onDestroy() {
weakReference = null;
}
protected T getView() {
T view = weakReference != null ? (T) weakReference.get() : null;
if (view == null) {
return getEmptyView();
}
return view;
}
protected abstract T getEmptyView();
}
IMvpView的作用就是获取代理类
public interface IMvpView {
MvpControler getMvpControler();
}
MvpControler 代理P层具体实现
public class MvpControler implements ILifeCircle {
//存放的是 P层的实例
private Set lifeCircles = new HashSet<>();
public void savePresenter(ILifeCircle lifeCircle) {
this.lifeCircles.add(lifeCircle);
}
@Override
public void onCreate(Bundle savedInstanceState, Intent intent, Bundle getArguments) {
Iterator iterator = this.lifeCircles.iterator();
while (iterator.hasNext()) {
ILifeCircle next = iterator.next();
if (intent == null) {
intent = new Intent();
}
if (getArguments == null) {
getArguments = new Bundle();
}
next.onCreate(savedInstanceState,intent,getArguments);
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState, Intent extras, Bundle getArguments) {
Iterator var3 = this.lifeCircles.iterator();
while (var3.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var3.next();
if (extras == null) {
extras = new Intent();
}
if (getArguments == null) {
getArguments = new Bundle();
}
presenter.onActivityCreated(savedInstanceState, extras,getArguments);
}
}
@Override
public void onDestroy() {
Iterator var1 = this.lifeCircles.iterator();
while (var1.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var1.next();
presenter.onDestroy();
}
this.lifeCircles.clear();
}
@Override
public void onViewDestroy() {
Iterator var1 = this.lifeCircles.iterator();
while (var1.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var1.next();
presenter.onViewDestroy();
}
}
@Override
public void onResume() {
Iterator var2 = this.lifeCircles.iterator();
while (var2.hasNext()) {
ILifeCircle presenter1 = (ILifeCircle) var2.next();
presenter1.onResume();
}
}
@Override
public void onPause() {
Iterator var1 = this.lifeCircles.iterator();
while (var1.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var1.next();
presenter.onPause();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
Iterator var2 = this.lifeCircles.iterator();
while (var2.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var2.next();
presenter.onSaveInstanceState(outState);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Iterator var4 = this.lifeCircles.iterator();
while (var4.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var4.next();
presenter.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void attachView(IMvpView iMvpView) {
Iterator var1 = this.lifeCircles.iterator();
while (var1.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var1.next();
presenter.attachView(iMvpView);
}
}
@Override
public void destroyView() {
Iterator var1 = this.lifeCircles.iterator();
while (var1.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var1.next();
presenter.destroyView();
}
}
@Override
public void onStart() {
Iterator var1 = this.lifeCircles.iterator();
while (var1.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var1.next();
presenter.onStart();
}
}
@Override
public void onStop() {
Iterator var1 = this.lifeCircles.iterator();
while (var1.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var1.next();
presenter.onStop();
}
}
@Override
public void onNewIntent(Intent intent) {
Iterator var2 = this.lifeCircles.iterator();
while (var2.hasNext()) {
ILifeCircle presenter = (ILifeCircle) var2.next();
presenter.onNewIntent(intent);
}
}
}
p层的中间类
public abstract class BaseMvpPresenter extends LifeCircleMvpPresenter{
public BaseMvpPresenter(T view) {
super(view);
}
@Override
public void onCreate(Bundle savedInstanceState, Intent intent, Bundle getArguments) {
}
@Override
public void onActivityCreated(Bundle savedInstanceState, Intent intent, Bundle getArguments) {
}
@Override
public void onStart() {
}
@Override
public void onResume() {
}
@Override
public void onPause() {
}
@Override
public void onStop() {
}
@Override
public void destroyView() {
}
@Override
public void onViewDestroy() {
}
@Override
public void onNewIntent(Intent intent) {
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
}
@Override
public void onSaveInstanceState(Bundle bundle) {
}
}
public class LifeCircleMvpActivity extends AppCompatActivity implements IMvpView{
private MvpControler mvpControler;
@Override
public MvpControler getMvpControler() {
if (this.mvpControler == null) {
this.mvpControler = new MvpControler();
}
return this.mvpControler;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = this.getIntent();
if (intent == null) {
intent = new Intent();
}
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onCreate(savedInstanceState,intent,null);
mvpControler.onActivityCreated(savedInstanceState,intent,null);
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onNewIntent(intent);
}
}
@Override
protected void onStart() {
super.onStart();
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onStart();
}
}
@Override
protected void onResume() {
super.onResume();
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onResume();
}
}
@Override
protected void onPause() {
super.onPause();
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onPause();
}
}
@Override
protected void onStop() {
super.onStop();
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onStop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onDestroy();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onSaveInstanceState(outState);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
MvpControler mvpControler = this.getMvpControler();
if (mvpControler != null) {
mvpControler.onActivityResult(requestCode,resultCode,data);
}
}
}