MVP架構(gòu)在Android這一塊已經(jīng)盛行依舊,對(duì)于一些學(xué)習(xí)能力比較強(qiáng)的人來(lái)說(shuō),已經(jīng)能夠運(yùn)用自如甚至改造優(yōu)化了,對(duì)于吾等菜鳥,卻是如此的陌生,今日這篇博客,算是小弟在學(xué)習(xí)和應(yīng)用上的一點(diǎn)總結(jié)罷了,如有不足,還請(qǐng)各位大神不吝指教。
MVP架構(gòu)是什么就不多說(shuō)了,博主主要很大家分享的是,如何設(shè)計(jì)MVP架構(gòu)。
先來(lái)分析一下MVP如何使用:M-V-P三層之間,P作為中間層,負(fù)責(zé)M,V之間的數(shù)據(jù)交互的中介,將數(shù)據(jù)從M層獲取,處理之后提交到V層,換句話說(shuō),V需要持有P的實(shí)例,P層需要持有V的實(shí)例。原理很簡(jiǎn)單,使用泛型對(duì)數(shù)據(jù)進(jìn)行封裝處理:
1.定義一個(gè)V層的空接口,主要是方便封裝:
/**
* V層接口
*/ public interface IView { }
2.定義一個(gè)P層的接口:
/**
* 抽象為接口
*
*/ public interface IPresenter<V extends IView> { /**
* 綁定視圖
*
* @param view
*/ void attachView(V view); /**
* 解除綁定(每個(gè)V記得使用完之后解綁,主要是用于防止內(nèi)存泄漏問(wèn)題)
*/ void dettachView();
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
3.封裝P基類:綁定解綁V實(shí)例
/**
* 抽象類 統(tǒng)一管理View層綁定和解除綁定
*
* @param <V>
*/ public class BasePresenter<V extends IView, M extends IModel> implements IPresenter<V> { private WeakReference<V> weakView; protected M model;
public V getView() { return proxyView;
} /**
* 用于檢查View是否為空對(duì)象
*
* @return */ public boolean isAttachView() { return this.weakView != null && this.weakView.get() != null;
} @Override public void attachView(V view) { this.weakView = new WeakReference<V>(view);
} @Override public void dettachView() { if (this.weakView != null) { this.weakView.clear(); this.weakView = null;
}
}
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
4.M層封裝:
/**
* M層
*/ public interface IModel { } /**
* 登錄model
* Created by admin on 2018/2/5.
*/ public interface ILoginModel extends IModel { void login();
} /**
* 登錄
* Created by admin on 2018/2/5.
*/ public class LoginModel implements ILoginModel { @Override public void login() { }
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
之后,將數(shù)據(jù)提交到activity或者fragment就行了。
最基本的鋪墊已經(jīng)做好了,接下來(lái)就該封裝View了:
public abstract class MvpActivity<V extends IView, P extends BasePresenter<V>> extends AppCompatActivity implements IView { private P presenter;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
...
presenter=getPresenter();
presenter.attachView(this);
} protected P getPresenter() { return presenter;
} protected void setPresenter(P presenter) { this.presenter = presenter;
} protected V getView() { return (V) this;
}
...
@Override protected void onDestroy() {
presenter.dettachView();
... super.onDestroy();
}
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
收工,MVP基礎(chǔ)框架搭建完成了。沒(méi)錯(cuò),就是基礎(chǔ)框架,但是能不能用呢,讓我們拭目以待吧。
先來(lái)寫一個(gè)View:
public interface ILoginView extends IView { void onLoginSuccess(); void onFailed();
}
然后是Presneter:
/**
* Created by admin on 2018/2/5.
*/ public class LoginPresenter extends BasePresenter<ILogin, LoginModel> { public LoginPresenter() {
model = new LoginModel();
}
public void login(){
model.login(new LoginCallBack() { @Override public void onSuccess() { if(null!=(ILogin)getView()){
weakView.onLoginSuccess();
}
} @Override public void onFailure() { if(null!=(ILogin)getView()){
weakView.onFailure();
}
}
});
}
}
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
最后來(lái)完成Activity的邏輯:
public class LoginActivity extends MvpActivity<ILoginView, LoginPresenter> implements ILoginView { ...
@Override public LoginPresenter getPresenter() { return new LoginPresenter();
} public void login(View view) {
String name = etUserName.getText().toString();
String pwd = etUserPwd.getText().toString();
getPresenter().login(name, pwd);
}
@Override public void onLoginSuccess() {
}
@Override public void onFailed(){
...
}