MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑汇集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不须要从新编写业务逻辑。其中M层处理数据,业务逻辑等;V层处理界面的显示结果;C层起到桥梁的做用,来控制V层和M层通讯以此来达到分离视图显示和业务逻辑层。说了这么多,听着感受很抽象,废话很少说,咱们来看看MVC在Android开发中是怎么应用的吧!php
在Android开发中,比较流行的开发框架模式采用的是MVC框架模式,采用MVC模式的好处是便于UI界面部分的显示和业务逻辑,数据处理分开。那么Android项目中哪些代码来充当M,V,C角色呢?html
M层:适合作一些业务逻辑处理,好比 数据库存取操做,网络操做,复杂的算法,耗时的任务等都在model层处理。 V层:应用层中处理数据显示的部分,XML布局能够视为V层,显示Model层的数据结果。 C层:在Android中,Activity处理用户交互问题,所以能够认为Activity是控制器,Activity读取V视图层的数据(eg.读取当前EditText控件的数据),控制用户输入(eg.EditText控件数据的输入),并向Model发送数据请求(eg.发起网络请求等)。接下来咱们经过一个获取天气预报数据的小项目来解读 MVC for Android。先上一个界面图:java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
<code
class
=
"hljs"
java=
""
>
package
com.xjp.androidmvcdemo.controller;
import
android.app.Dialog;
import
android.app.ProgressDialog;
import
android.os.Bundle;
import
android.support.v7.app.ActionBarActivity;
import
android.view.View;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import
com.xjp.androidmvcdemo.R;
import
com.xjp.androidmvcdemo.entity.Weather;
import
com.xjp.androidmvcdemo.entity.WeatherInfo;
import
com.xjp.androidmvcdemo.model.OnWeatherListener;
import
com.xjp.androidmvcdemo.model.WeatherModel;
import
com.xjp.androidmvcdemo.model.WeatherModelImpl;
public
class
MainActivity
extends
ActionBarActivity
implements
OnWeatherListener, View.OnClickListener {
private
WeatherModel weatherModel;
private
Dialog loadingDialog;
private
EditText cityNOInput;
private
TextView city;
private
TextView cityNO;
private
TextView temp;
private
TextView wd;
private
TextView ws;
private
TextView sd;
private
TextView wse;
private
TextView time;
private
TextView njd;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weatherModel =
new
WeatherModelImpl();
initView();
}
/**
* 初始化View
*/
private
void
initView() {
cityNOInput = findView(R.id.et_city_no);
city = findView(R.id.tv_city);
cityNO = findView(R.id.tv_city_no);
temp = findView(R.id.tv_temp);
wd = findView(R.id.tv_WD);
ws = findView(R.id.tv_WS);
sd = findView(R.id.tv_SD);
wse = findView(R.id.tv_WSE);
time = findView(R.id.tv_time);
njd = findView(R.id.tv_njd);
findView(R.id.btn_go).setOnClickListener(
this
);
loadingDialog =
new
ProgressDialog(
this
);
loadingDialog.setTitle(加载天气中...);
}
/**
* 显示结果
*
* @param weather
*/
public
void
displayResult(Weather weather) {
WeatherInfo weatherInfo = weather.getWeatherinfo();
city.setText(weatherInfo.getCity());
cityNO.setText(weatherInfo.getCityid());
temp.setText(weatherInfo.getTemp());
wd.setText(weatherInfo.getWD());
ws.setText(weatherInfo.getWS());
sd.setText(weatherInfo.getSD());
wse.setText(weatherInfo.getWSE());
time.setText(weatherInfo.getTime());
njd.setText(weatherInfo.getNjd());
}
/**
* 隐藏进度对话框
*/
public
void
hideLoadingDialog() {
loadingDialog.dismiss();
}
@Override
public
void
onClick(View v) {
switch
(v.getId()) {
case
R.id.btn_go:
loadingDialog.show();
weatherModel.getWeather(cityNOInput.getText().toString().trim(),
this
);
break
;
}
}
@Override
public
void
onSuccess(Weather weather) {
hideLoadingDialog();
displayResult(weather);
}
@Override
public
void
onError() {
hideLoadingDialog();
Toast.makeText(
this
, 获取天气信息失败, Toast.LENGTH_SHORT).show();
}
private
<t
extends
=
""
view=
""
> T findView(
int
id) {
return
(T) findViewById(id);
}
}
</t></code>
|
从上面代码能够看到,Activity持有了WeatherModel模型的对象,当用户有点击Button交互的时候,Activity做为Controller控制层读取View视图层EditTextView的数据,而后向Model模型发起数据请求,也就是调用WeatherModel对象的方法 getWeathre()方法。当Model模型处理数据结束后,经过接口OnWeatherListener通知View视图层数据处理完毕,View视图层该更新界面UI了。而后View视图层调用displayResult()方法更新UI。至此,整个MVC框架流程就在Activity中体现出来了。android
来看看WeatherModelImpl代码实现算法
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
<code
class
=
"hljs"
java=
""
>
package
com.xjp.androidmvcdemo.model;
/**
* Description:请求网络数据接口
* User: xjp
* Date: 2015/6/3
* Time: 15:40
*/
public
interface
WeatherModel {
void
getWeather(String cityNumber, OnWeatherListener listener);
}
................
package
com.xjp.androidmvcdemo.model;
import
com.android.volley.Response;
import
com.android.volley.VolleyError;
import
com.xjp.androidmvcdemo.entity.Weather;
import
com.xjp.androidmvcdemo.volley.VolleyRequest;
/**
* Description:从网络获取天气信息接口实现
* User: xjp
* Date: 2015/6/3
* Time: 15:40
*/
public
class
WeatherModelImpl
implements
WeatherModel {
@Override
public
void
getWeather(String cityNumber,
final
OnWeatherListener listener) {
/*数据层操做*/
VolleyRequest.newInstance().newGsonRequest(http:
//www.weather.com.cn/data/sk/ + cityNumber + .html,
Weather.
class
,
new
Response.Listener<weather>() {
@Override
public
void
onResponse(Weather weather) {
if
(weather !=
null
) {
listener.onSuccess(weather);
}
else
{
listener.onError();
}
}
},
new
Response.ErrorListener() {
@Override
public
void
onErrorResponse(VolleyError error) {
listener.onError();
}
});
}
}
</weather></code>
|
以上代码看出,这里设计了一个WeatherModel模型接口,而后实现了接口WeatherModelImpl类。controller控制器activity调用WeatherModelImpl类中的方法发起网络请求,而后经过实现OnWeatherListener接口来得到网络请求的结果通知View视图层更新UI 。至此,Activity就将View视图显示和Model模型数据处理隔离开了。activity担当contronller完成了model和view之间的协调做用。数据库
至于这里为何不直接设计成类里面的一个getWeather()方法直接请求网络数据?你考虑下这种状况:如今代码中的网络请求是使用Volley框架来实现的,若是哪天老板非要你使用Afinal框架实现网络请求,你怎么解决问题?难道是修改 getWeather()方法的实现? no no no,这样修改不只破坏了之前的代码,并且还不利于维护, 考虑到之后代码的扩展和维护性,咱们选择设计接口的方式来解决着一个问题,咱们实现另一个WeatherModelWithAfinalImpl类,继承自WeatherModel,重写里面的方法,这样不只保留了之前的WeatherModelImpl类请求网络方式,还增长了WeatherModelWithAfinalImpl类的请求方式。Activity调用代码无须要任何修改。canvas
利用MVC设计模式,使得这个天气预报小项目有了很好的可扩展和维护性,当须要改变UI显示的时候,无需修改Contronller(控制器)Activity的代码和Model(模型)WeatherModel模型中的业务逻辑代码,很好的将业务逻辑和界面显示分离。设计模式
在Android项目中,业务逻辑,数据处理等担任了Model(模型)角色,XML界面显示等担任了View(视图)角色,Activity担任了Contronller(控制器)角色。contronller(控制器)是一个中间桥梁的做用,经过接口通讯来协同 View(视图)和Model(模型)工做,起到了二者之间的通讯做用。网络
何时适合使用MVC设计模式?固然一个小的项目且无需频繁修改需求就不用MVC框架来设计了,那样反而以为代码过分设计,代码臃肿。通常在大的项目中,且业务逻辑处理复杂,页面显示比较多,须要模块化设计的项目使用MVC就有足够的优点了。mvc
4.在MVC模式中咱们发现,其实控制器Activity主要是起到解耦做用,将View视图和Model模型分离,虽然Activity起到交互做用,可是找Activity中有不少关于视图UI的显示代码,所以View视图和Activity控制器并非彻底分离的,也就是说一部分View视图和Contronller控制器Activity是绑定在一个类中的。
MVC的优势:
(1)耦合性低。所谓耦合性就是模块代码之间的关联程度。利用MVC框架使得View(视图)层和Model(模型)层能够很好的分离,这样就达到了解耦的目的,因此耦合性低,减小模块代码之间的相互影响。
(2)可扩展性好。因为耦合性低,添加需求,扩展代码就能够减小修改以前的代码,下降bug的出现率。
(3)模块职责划分明确。主要划分层M,V,C三个模块,利于代码的维护。