所謂MVP(Model-View-Presenter)模式。是將APP的結構分爲三層:java
view 層主要負責:android
presenter 層主要負責:git
model層主要負責:github
如圖示,裏面的activity,presenter,model均爲例子:數據庫
將複雜的功能分割爲各層內的小問題。各層內功能單一。這樣易於功能修改拓展與Debug。
解耦的設計,獨立的模塊,更有利於分工開發與測試。網絡
簡單例子以下app
UserBean.javaide
package com.example.mvptest.com.example.bean; /** * Created by Administrator on 2016/6/2. */ public class UserBean { private String mobile; private String userpwd; public UserBean(String mobile, String userpwd) { this.mobile = mobile; this.userpwd = userpwd; } public String getMobile() { return mobile; } public String getUserpwd() { return userpwd; } }
Model層測試
IUserModel.java和UserModel.javathis
public interface IUserModel { void setID(int id); void setMobile(String mobile); void setUserPwd(String userpwd); UserBean load(int id); }
package com.example.mvptest.com.example.model; import android.util.SparseArray; import com.example.mvptest.com.example.bean.UserBean; /** * Created by Administrator on 2016/6/2. */ public class UserModel implements IUserModel { private String usermobile; private String userPwd; private int mID; private SparseArray<UserBean> mUserArray = new SparseArray<UserBean>(); @Override public void setID(int id) { mID = id; } @Override public void setMobile(String mobile) { usermobile = mobile; } @Override public void setUserPwd(String userpwd) { userPwd = userpwd; UserBean userBean = new UserBean(usermobile, userpwd); mUserArray.append(mID, userBean); } @Override public UserBean load(int id) { mID = id; UserBean userBean = mUserArray.get(mID, new UserBean("no data", "not data")); return userBean; } }
View層
package com.example.mvptest.com.example.view; /** * Created by Administrator on 2016/6/2. */ public interface IUserView { int getID(); String getMobile(); String getUserPwd(); void setMobile(String mobile); void setUserpwd(String userpwd); }
Presenter層
package com.example.mvptest.com.example.presenter; import com.example.mvptest.com.example.bean.UserBean; import com.example.mvptest.com.example.model.IUserModel; import com.example.mvptest.com.example.model.UserModel; import com.example.mvptest.com.example.view.IUserView; /** * Created by Administrator on 2016/6/2. */ public class UserPresenter { private IUserView mUserView; private IUserModel mUserModel; public UserPresenter(IUserView mUserView) { this.mUserView = mUserView; this.mUserModel = new UserModel(); } public void saveUser(int id, String mobile, String userpwd) { mUserModel.setID(id); mUserModel.setMobile(mobile); mUserModel.setUserPwd(userpwd); } public void loadUser(int id) { UserBean user = mUserModel.load(id); mUserView.setMobile("bb"+user.getMobile()); mUserView.setUserpwd(user.getUserpwd()); } }
Activity層實現IUserView接口
public class MainActivity extends AppCompatActivity implements View.OnClickListener, IUserView{ private EditText mobileEdt, pwdEdt; UserPresenter presenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.save).setOnClickListener(this); findViewById(R.id.load).setOnClickListener(this); mobileEdt = (EditText) findViewById(R.id.mobile); pwdEdt = (EditText) findViewById(R.id.passwd); presenter = new UserPresenter(this); } @Override public int getID() { return 3; } @Override public String getMobile() { return mobileEdt.getText().toString(); } @Override public String getUserPwd() { return pwdEdt.getText().toString(); } @Override public void setMobile(String mobile) { mobileEdt.setText(mobile); } @Override public void setUserpwd(String userpwd) { pwdEdt.setText(userpwd); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.save: presenter.saveUser(3, getMobile(), getUserPwd()); break; case R.id.load: presenter.loadUser(3); break; default: break; } } }
複雜例子:https://github.com/wenhuaijun/SearchPictureTool