最近看了一下Android开发的MVP模式以及接口,深有体会。
如图是MVP的一种简单结构,源码来自网上的例子
(http://blog.csdn.net/knxw0001/article/details/39637273)
1.
package com.mvp.bean;
//用户数据
public class UserBean {
private String mFirstName;
private String mLastName;
public UserBean(String firstName, String lastName) {
// TODO Auto-generated constructor stub
this.mFirstName = firstName;
this.mLastName = lastName;
}
public String getFirstName() {
return mFirstName;
}
public String getLastName() {
return mLastName;
}
}
2.
package com.mvp.model;
import com.mvp.bean.UserBean;
public interface IUserModel {
void setID(int id);
void setFirstName(String firstName);
void setLastName(String lastName);
UserBean load(int id);//接口中的方法没有具体实现,只是声明
}
3.
package com.mvp.model;
import android.util.SparseArray;
import com.mvp.bean.UserBean;
//用户modol继承了接口
public class UserModel implements IUserModel {
private String mFristName;
private String mLastName;
private int mID;
private SparseArray<UserBean> mUsererArray = new SparseArray<UserBean>();
//下面都是重写的接口中的方法
@Override
public void setID(int id) {
// TODO Auto-generated method stub
mID = id;
}
@Override
public void setFirstName(String firstName) {
// TODO Auto-generated method stub
mFristName = firstName;
}
@Override
public void setLastName(String lastName) {
// TODO Auto-generated method stub
mLastName = lastName;
UserBean UserBean = new UserBean(mFristName, mLastName);
mUsererArray.append(mID, UserBean);
}
@Override//接口方法的具体实现
public UserBean load(int id) {
// TODO Auto-generated method stub
mID = id;
UserBean userBean = mUsererArray.get(mID, new UserBean("not found",
"not found"));
return userBean;
}
}
4.
package com.mvp.presenter;
import com.mvp.bean.UserBean;
import com.mvp.model.IUserModel;
import com.mvp.model.UserModel;
import com.mvp.view.IUserView;
public class UserPresenter {
private IUserView mUserView;//这两个都是接口,通过实例化接口对象实现了“引用”,调用继承了该接口的类的方法
private IUserModel mUserModel;
public UserPresenter(IUserView view) {
mUserView = view;//muserview是一个接口,不需要新建对象
mUserModel = new UserModel();
}
public void saveUser(int id, String firstName, String lastName) {
mUserModel.setID(id);
mUserModel.setFirstName(firstName);
mUserModel.setLastName(lastName);
}
public void loadUser(int id) {
UserBean user = mUserModel.load(id);
mUserView.setFirstName(user.getFirstName());
mUserView.setLastName(user.getLastName());
}
}
5.
package com.mvp.view;
public interface IUserView {
int getID();
String getFristName();
String getLastName();
void setFirstName(String firstName);
void setLastName(String lastName);
}
6.
package com.mvp.view;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.mvp.R;
import com.mvp.presenter.UserPresenter;
public class UserActivity extends Activity implements OnClickListener,
IUserView {
private EditText mFirstNameEditText, mLastNameEditText, mIdEditText;
private Button mSaveButton, mLoadButton;
private UserPresenter mUserPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findWidgets();
mUserPresenter = new UserPresenter(this);
mSaveButton.setOnClickListener(this);
mLoadButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.saveButton:
mUserPresenter.saveUser(getID(), getFristName(),
getLastName());
break;
case R.id.loadButton:
mUserPresenter.loadUser(getID());
break;
default:
break;
}
}
@Override
public void setFirstName(String firstName) {
// TODO Auto-generated method stub
mFirstNameEditText.setText(firstName);
}
@Override
public void setLastName(String lastName) {
// TODO Auto-generated method stub
mLastNameEditText.setText(lastName);
}
@Override
public int getID() {
// TODO Auto-generated method stub
return Integer.parseInt(mIdEditText.getText().toString());
}
@Override
public String getFristName() {
// TODO Auto-generated method stub
return mFirstNameEditText.getText().toString();
}
@Override
public String getLastName() {
// TODO Auto-generated method stub
return mLastNameEditText.getText().toString();
}
void findWidgets() {
mFirstNameEditText = (EditText) findViewById(R.id.first_name_edt);
mLastNameEditText = (EditText) findViewById(R.id.last_name_edt);
mIdEditText = (EditText) findViewById(R.id.id_edt);
mSaveButton = (Button) findViewById(R.id.saveButton);
mLoadButton = (Button) findViewById(R.id.loadButton);
}
}
代码中的注释已经说明了一切。
需要注意的:实例化的接口对象是为了能调用继承了该接口的类的方法;
继承接口的类必须实现接口中的所有方法,接口中的方法并不会写具体内容,只是相当于一个“声明”。
注意到了这些,整体 架构 的逻辑就十分好理解了,希望对 Java 语法理解不深的android初学者可以好好体会一下