mvc从分析到实例,实例到总结

来源:互联网 发布:淘宝代销经营地址 编辑:程序博客网 时间:2024/05/18 03:52
采取一个实例我们来了解mvc模式的使用,通过显示天气信息。当然,在实例之前我们先分析mvc的逻辑和每个层他们的职责.
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。其中M层处理数据,业务逻辑等;V层处理界面的显示结果;C层起到桥梁的作用,来控制V层和M层通信以此来达到分离视图显示和业务逻辑层。
MVC for Android:
M层:适合做一些业务逻辑处理,比如数据库存取操作,网络操作,复杂的算法,耗时的任务等都在model层处理。 V层:应用层中处理数据显示的部分,XML布局可以视为V层,显示Model层的数据结果。 C层:在Android中,Activity处理用户交互问题,因此可以认为Activity是控制器,Activity读取V视图层的数据(eg.读取当前EditText控件的数据),控制用户输入(eg.EditText控件数据的输入),并向Model发送数据请求(eg.发起网络请求等)。
那么接下来我们使用一个小天气来让大家增加印象:

Controller:activity
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);    }}
Model:数据库存取操作,网络操作,复杂的算法,耗时的任务
public interface WeatherModel {    void getWeather(String cityNumber, OnWeatherListener listener);}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();                    }                });    }}
MVC使用总结:
在Android项目中,业务逻辑,数据处理等担任了Model(模型)角色,XML界面显示等担任了View(视图)角色,Activity担任了Contronller(控制器)角色。contronller(控制器)是一个中间桥梁的作用,通过接口通信来协同 View(视图)和Model(模型)工作,起到了两者之间的通信作用。

什么时候适合使用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三个模块,利于代码的维护。
1 0