這篇文章記錄一下Android中框架的Mvc擒抛、Mvp、Mvvm模式补疑。在面試中有很多面試官都會問到講講三種模式的運用和區(qū)別歧沪,所以自己就來總結(jié)一下自己理解的相關(guān)的內(nèi)容。
1莲组、MVC
Model(模型層):主要是對數(shù)據(jù)相關(guān)的操作在Model層中诊胞,比如網(wǎng)絡(luò)請求、數(shù)據(jù)庫相關(guān)內(nèi)容
View(視圖層):一般通過xml界面展示出來的與用戶交互的UI展示
Controller(控制層):主要是進行邏輯操作的相關(guān)內(nèi)容锹杈,Android中一般activity為控制器撵孤。
實現(xiàn):
Controller:
public class OneActivity extends BaseAcvitiy {
private OneModel oneModel;
private TextView textView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//View的展現(xiàn)
setContentView(R.layout.activity_one);
//Model
oneModel = new OneModel();
textView = findViewById(R.id.tv);
textView.setOnClickListener(onClickListener);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
//進行邏輯調(diào)用,點擊view獲取數(shù)據(jù)操作
oneModel.invoke(1000, oneCallBack);
}
};
OneCallBack oneCallBack = new OneCallBack() {
@Override
public void onSuccess(String data) {
textView.setText(data);
}
@Override
public void onFial(String errorMessage) {
textView.setText(errorMessage);
}
};
}
View:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="請求數(shù)據(jù)"
android:textSize="24sp"/>
</android.support.constraint.ConstraintLayout>
Model:
public class OneModel {
public void invoke(int time, OneCallBack oneCallBack) {
setOneCallBack(oneCallBack);
//仿網(wǎng)絡(luò)請求竭望,嘿
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
//網(wǎng)絡(luò)請求狀態(tài)判斷
if (getOneCallBack() != null) {
switch (time) {
case 1000:
getOneCallBack().onSuccess("請求成功:" +
"Find something you love doing and then find a way to scale it.");
break;
case 3000:
getOneCallBack().onFial("請求超時");
break;
}
}
}
private OneCallBack oneCallBack;
public OneCallBack getOneCallBack() {
return oneCallBack;
}
public void setOneCallBack(OneCallBack oneCallBack) {
this.oneCallBack = oneCallBack;
}
}
在MVC中邪码,Activity/Fragment既充當(dāng)了控制器層,又充當(dāng)了VIew層咬清,導(dǎo)致耦合性很高闭专,如果項目的需求功能越來越多,Activity/Fragment中的代碼會越來越龐大旧烧,會導(dǎo)致難以維護和測試影钉。所以就會有了后面的MVP的架構(gòu)模式的出現(xiàn)。
2粪滤、MVP
Model(模型層):同MVC中的Model層
View(視圖層):同MVC中的VIew層
Presenter(控制層):主要是進行邏輯的操作斧拍,來支持View和Model之間的交互
實現(xiàn):
Model:
public interface IModel {
void network(int time,TwoCallBack twoCallBack);
}
public class TwoModel implements IModel {
@Override
public void network(int time, TwoCallBack twoCallBack) {
setTwoCallBack(twoCallBack);
//仿網(wǎng)絡(luò)請求,嘿
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
//網(wǎng)絡(luò)請求狀態(tài)判斷
if (getOneCallBack() != null) {
switch (time) {
case 1000:
getOneCallBack().onSuccess("請求成功:" +
"Find something you love doing and then find a way to scale it.");
break;
case 3000:
getOneCallBack().onFial("請求超時");
break;
}
}
}
private TwoCallBack twoCallBack;
public TwoCallBack getOneCallBack() {
return twoCallBack;
}
public void setTwoCallBack(TwoCallBack twoCallBack) {
this.twoCallBack = twoCallBack;
}
}
View:
public interface IView {
void success(String data);
void fail(String data);
}
Presenter:
public interface IPresenter {
void init(IView iView);
void invoke();
}
public class TwoPresenter implements IPresenter {
private IView iView;
private IModel iModel;
@Override
public void init(IView iView) {
this.iView = iView;
iModel = new TwoModel();
}
@Override
public void invoke() {
iModel.network(1000,twoCallBack );
}
TwoCallBack twoCallBack = new TwoCallBack() {
@Override
public void onSuccess(String data) {
if (iView != null) {
iView.success(data);
}
}
@Override
public void onFial(String errorMessage) {
if (iView != null) {
iView.fail(errorMessage);
}
}
};
}
Activity調(diào)用:
public class TwoActivity extends BaseAcvitiy implements IView {
private Button button;
private TwoPresenter presenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//View
setContentView(R.layout.activity_two);
//Presenter初始化
presenter = new TwoPresenter();
presenter.init(this);
button = findViewById(R.id.bt);
button.setOnClickListener(onClickListener);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
//調(diào)用邏輯實現(xiàn)
presenter.invoke();
}
};
@Override
public void success(String data) {
button.setText(data);
}
@Override
public void fail(String errorMs) {
button.setText(errorMs);
}
}
在MVP模式中杖小,主要的特點是面向接口編程的肆汹。它將業(yè)務(wù)從Activity/Fragment中分離出去,把功能邏輯操作都放在了Persenter層予权,Activity/Fragment只負(fù)責(zé)視圖層的展示昂勉,VIew層和Model層沒有任何交互,降低了耦合扫腺,更便于后期項目的維護和開發(fā)岗照。
3、MVVM
Model(模型層):同MVC中的Model層
View(視圖層):同MVC中的VIew層
ViewModel(視圖模型層):處理邏輯相關(guān)內(nèi)容操作
Model層:
public class ThreeModel {
public ObservableField<String> data=new ObservableField<>();
public void invoke(int time,IModel iModel){
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
iModel.onSuccess();
}
View層:
public class ThreeActivity extends BaseAcvitiy {
private ActivityThreeBinding activityThreeBinding;
private ThreeViewModel threeModel;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityThreeBinding = DataBindingUtil
.setContentView(this,R.layout.activity_three);
threeModel = new ThreeViewModel(activityThreeBinding);
activityThreeBinding.setModel(threeModel.getData());
activityThreeBinding.setMain(ThreeActivity.this);
}
public void onClickTv(View view){
threeModel.invoke();
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="model"
type="com.wtt.testac.mvvm.model.ThreeModel" />
<variable
name="main"
type="com.wtt.testac.mvvm.view.ThreeActivity" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{model.data}"
android:onClick="@{main.onClickTv}"/>
</android.support.constraint.ConstraintLayout>
</layout>
ViewModel層:
public class ThreeViewModel {
private ThreeModel threeModel;
public ThreeViewModel(ActivityThreeBinding activityThreeBinding) {
threeModel = new ThreeModel();
}
public ThreeModel getData() {
threeModel.data.set("請求數(shù)據(jù)");
return threeModel;
}
public void invoke() {
threeModel.invoke(1000, iModel);
}
IModel iModel = new IModel() {
@Override
public void onSuccess() {
threeModel.data.set("請求成功");
}
@Override
public void onFial(String errorMessage) {
}
};
}
在MVVM中,View和Model使用DataBinding來進行雙向綁定攒至,一方的改變都會影響另一方厚者,開發(fā)者不用再去手動修改UI的數(shù)據(jù)。項目結(jié)構(gòu)更加的低耦合迫吐。
總結(jié):這篇文章主要介紹了三種不同模式的區(qū)別和簡單的例子實現(xiàn)库菲,具體項目用什么模式可以自己針對具體情況去選擇合適的模式。文章中有任何問題志膀,和不對的地方熙宇,歡迎指出,后續(xù)會繼續(xù)完善溉浙。