MVP的详解

之前我们在Android中很多时候用的MVC框架,它也实现了解耦作用但是在Model和View之间他们是双向可取的,所以还是存在一定的耦合,所以出现了MVP框架,而MVP实现了他们之间的解耦,因为View--->Present--->Model,其中View和Model通过Present来获取数据,之间没有直接的关联,可是很多人会说你只是通过一个类将他们单独的提取出来然后在Present这个类中进行数据交换不还是存在耦合吗?这里我们是通过接口来实现数据的交互的,所以就不会出现刚才说的那种情况,现在我们通过代码来边写边说:

首先我们先写一个获取天气的小例子:

一、接口

1.先写一个View接口,这里定义几个方法:

public interface WeatherView {
    void showload();//显示进度条的抽象方法
    void hideload();//隐藏<span style="font-family: Arial, Helvetica, sans-serif;">进度条的抽象方法</span>
    void showError();<span style="font-family: Arial, Helvetica, sans-serif;">//获取数据出错时显示的方法</span>
    void setWeatherInfo(Weather weather);//获取到数据并显示的方法
}
为什么要写一个借口呢?主要是为了在Present实现类进行数据交换,解耦。

2.写一个Present接口,定义一个获取参数的方法:

public interface WeatherPresenter {
    void getWeather(String cityNo);
}
此接口中的方法主要用于获取到参数,并将参数传递给Model逻辑操作中,实现逻辑获取数据

3.写一个Model接口,定义一个加载数据的方法:

public interface WeatherModel {
     void loadWeather(String cityNo,OnWeatherListener listener);
}
4.在Model中的方法中看到我们传入了一个监听器的实例,那么现在需要将这个监听器接口也写出:

public interface OnWeatherListener {
    void onSuccess(Weather weather);
    void onError();
}
当获取数据成功时,onSuccess方法中将这个数据实例保存,待用。

二、实现相应的接口

1.MainActivity.java

public class MainActivity extends Activity implements WeatherView,View.OnClickListener{
    private EditText cityNOInput;
    private TextView city;
    private TextView cityNO;
    private TextView temp;
    private TextView wd;
    private TextView ws;
    private TextView sd;
    private Button btn;

    private WeatherPresenter weatherPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        addlistener();
    }

    private void addlistener() {
        btn.setOnClickListener(this);
    }
    private void init() {
        cityNOInput = (EditText) findViewById(R.id.et_city_no);
        city = (TextView) findViewById(R.id.tv_city);
        cityNO = (TextView) findViewById(R.id.tv_city_no);
        temp = (TextView) findViewById(R.id.tv_temp);
        wd = (TextView) findViewById(R.id.tv_WD);
        ws = (TextView) findViewById(R.id.tv_WS);
        sd = (TextView) findViewById(R.id.tv_SD);
        btn= (Button) findViewById(R.id.btn_go);

        weatherPresenter=new WeatherPresenterImpl(this);//实例化WeatherView
    }
    @Override
    public void showload() {
   //显示DialogProgress
    }
    @Override
    public void hideload() {
   //隐藏<span style="font-family: Arial, Helvetica, sans-serif;">DialogProgress</span>
    }

    @Override
    public void showError() {
        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setWeatherInfo(Weather weather) {
     city.setText(weather.getCity());
        cityNO.setText(weather.getCityId());
        temp.setText(weather.getTemp());
        sd.setText(weather.getSD());
        wd.setText(weather.getWD());
        ws.setText(weather.getWS());
    }

    @Override
    public void onClick(View v) {
      if(v.getId()==R.id.btn_go){
          weatherPresenter.getWeather(cityNOInput.getText().toString());//调用此方法将Model中需要的参数传递进WeatherPresenterImpl,交给Model
      }
    }
}
2.WeatherPresenterImpl.java:

public class WeatherPresenterImpl implements OnWeatherListener, WeatherPresenter {
private WeatherModel weatherModel;
    private WeatherView weatherView;

    public WeatherPresenterImpl(WeatherView weatherView) {
        weatherModel=new WeatherModelImpl();
        this.weatherView = weatherView;
    }

    @Override
    public void onSuccess(Weather weather) {//获得Model层中通过OnSuccess方法传递进来获得的实体类信息
        weatherView.hideload();//隐藏加载框
        weatherView.setWeatherInfo(weather);//将获取到的信息在View上显示
    }

    @Override
    public void onError() {
        weatherView.hideload();
        weatherView.showError();
    }

    @Override
    public void getWeather(String cityNo) {
        weatherView.showload();//显示加载框
        weatherModel.loadWeather(cityNo,this);//调用Model层的获取数据方法,在Model中完成相应的逻辑
    }
}
3.WeatherModelImpl.java:
public class WeatherModelImpl implements WeatherModel {
    private RequestQueue requestQueue= MyApplication.getrequest();
    @Override
    public void loadWeather(String cityNo,final OnWeatherListener listener) {
        FastJsonRequest fastJsonRequest=new FastJsonRequest("", Weather.class, new com.android.volley.Response.Listener() {
            @Override
            public void onResponse(Object o) {
                if(o != null){
             listener.onSuccess((Weather) o);//将获取的数据通过此监听器的onSuccess()方法传递给WeatherPresenterImpl类中
                }
            }
        }, new com.android.volley.Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
            listener.onError();
            }
        });
        requestQueue.add(fastJsonRequest);
    }
//GsonRequest的調用同FastJsonRequest一樣
}
代码中的注释能让你理清楚它的内部传递模式。

完整Demo,请点击http://download.csdn.net/detail/fuzhongbin/9511988








猜你喜欢

转载自blog.csdn.net/fuzhongbin/article/details/51331411
MVP