前言
为什么要使用架构模式,有什么作用?
使用架构的目的是使程序模块化,做到模块内部的高聚合和模块之间的低耦合,降低各层之间的耦合性,达到解耦的目的,减少模块代码之间的相互影响,模块区域的分明,也更加方便开发人员的维护
一、MVC是什么?
MVC是Model(模型层)-View(视图层)-Controller(控制器曾)的简称
Android中的MVC
Model:实体类,负责处理数据的加载或者存储
View: 布局文件,负责界面数据的展示,与用户进行交互
Controller:Activity,负责逻辑业务的处理
MVC的缺点是什么?
在Android中activity可以说同时充当V层和C层,这就是的activity的负担会比较重,随着页面以及业务逻辑复杂度的不断提升,Acitivity的代码就会变得臃肿,不利于后期的维护。
二、MVP是什么?
为了解决MVC的问题,MVP就被提出来了
MVP 是Model(模型)-View(视图)-Presenter(主持人 可以理解为中介)
在MVP中M层和V层是没有直接联系的,通过P层来完成它们之间的交流
Android中的MVP
Model:实体类(负责处理数据的加载或者存储,处理逻辑业务逻辑)。
View:布局文件+Activity,负责界面数据的展示,与用户进行交互。
Presenter:中介,负责完成View与Model间的交互。
三、实例
MVC实例
实现一个用户登录的功能
文件结构
1.布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"tools:context=".LogonActivity"><EditTextandroid:id="@+id/et_logon_username"android:layout_width="match_parent"android:layout_height="wrap_content"/><EditTextandroid:id="@+id/et_logon_password"android:layout_width="match_parent"android:layout_height="wrap_content"/><Buttonandroid:layout_gravity="center"android:id="@+id/bt_logon"android:text="登录"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
2.实体类
public class User {private String username;private String password;public User(String username, String password) {this.username = username;this.password = password;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
3.Activity
public class LogonActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_logon);EditText username = findViewById(R.id.et_logon_username);EditText password = findViewById(R.id.et_logon_password);Button button = findViewById(R.id.bt_logon);//设置user数据User user = new User("张三","123456");button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//验证用户名和密码是否正确if (user.getUsername().equals(username)&&user.getPassword().equals(password)){Toast.makeText(LogonActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();}else {Toast.makeText(LogonActivity.this,"登陆失败",Toast.LENGTH_SHORT).show();}}});}}
MVP实例
实现一个用户登录的功能
文件结构
1.Model层
User实体类与MVC实体类一样
UserLogon 实现类
public class UserLogon implements UserLogonInterface {@Overridepublic void logon(String username, String password,UserLogonListener userLogonListener) {User user = new User();user.setUsername(username);user.setPassword(password);if (username.equals("张三")&&password.equals("123456")){userLogonListener.logonSuccess(user);}else {userLogonListener.logonFailed(user);}}}
UserLogonInterface 接口
public interface UserLogonInterface {void logon(String username, String password,UserLogonListener userLogonListener);}
UserLogonListener 监听
public interface UserLogonListener {void logonSuccess(User user);void logonFailed(User user);}
2.View层
布局
布局跟MVC布局一样,就不重复贴代码了。
LogonActivity 类
public class LogonActivity extends AppCompatActivity implements UserLogonViewInterface{private EditText username;private EditText password;private Button button;private UserLogonPresenter userLogonPresenter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_logon);//初始化控件username = findViewById(R.id.et_username);password = findViewById(R.id.et_password);button = findViewById(R.id.bt_logon);userLogonPresenter = new UserLogonPresenter(this);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {userLogonPresenter.logon();}});}@Overridepublic String getUsername() {return username.getText().toString();}@Overridepublic String getPassword() {return password.getText().toString();}@Overridepublic void onSuccess(User user) {Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();}@Overridepublic void onFailed(User user) {Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();}}
UserLogonViewInterface 类
public interface UserLogonViewInterface {String getUsername();String getPassword();void onSuccess(User user);void onFailed(User user);}
3.Presenter层
UserLogonPresenter 类
public class UserLogonPresenter {private UserLogonViewInterface userLogonViewInterface;private UserLogonInterface userLogonInterface;public UserLogonPresenter(UserLogonViewInterface userLogonViewInterface) {this.userLogonViewInterface = userLogonViewInterface;userLogonInterface = new UserLogon();}public void logon() {userLogonInterface.logon(userLogonViewInterface.getUsername(), userLogonViewInterface.getPassword(), new UserLogonListener() {@Overridepublic void logonSuccess(User user) {userLogonViewInterface.onSuccess(user);}@Overridepublic void logonFailed(User user) {userLogonViewInterface.onFailed(user);}});}}
MVP实例执行流程:
用户点击按钮触发监听,调用UserLogonPresenter类的logon方法,然后会调用UserLogon类中的logon方法,然后判断用户信息是否正确,回调UserLogonPresenter类的logonSuccess或者logonFailed方法,然后再次回调LogonActivity类的onSuccess或者onFailed方法,最后提示用户登陆成功还是失败