MVC
1. What is MVC in Android? Features?
Model
: Data structure and classes established for the business model (not related to View, only related to business)View
:XML/JAVA
OrJS+HTML
display the page.Activity/Frgament
Also assumes the function of View.Controller
:Android
The control layer is usually in itActivity、Fragment
.
The essence is the data of theController
operationModel
layer, and it will be数据
returned to theView
layer for display.
2. The disadvantages of Android MVC:
Activity
NotMVC
in the standardController
, bothController
duties haveView
responsibilities, resulting inActivity
code that is too bloated.View层
AndModel层
coupled to each other耦合过重
,代码量过大
and difficult to develop and maintain.
MVP
3. MVP mode in Android
MVP(Model-View-Presenter)
Model
: Mainly provide data storage function.Presenter
Need toModel
access data.View
: Responsible for processing点击事件和视图展示
(Activity、Fragment或者某个View控件
)Presenter
:View和Model
The bridge between,Model
after retrieving the data, returning to theView
layer. So thatM/V
there is no longer a coupling relationship.
4. What is the difference between MVP and MVC? (2)
MVP
View
Direct access is never allowed inModel
- The essence is to
增加了一个接口
lower one level耦合度
5. Features of MVP
Presenter
CompletelyModel
andView
decoupled, the main logic isPresenter
in.Presenter
It is具体View
not directly related to each other,接口
and interacts through a well-defined one .View
When changed, it can remainPresenter
unchanged (in line with the characteristics of object-oriented programming)View
There should only be simpleSet/Get
methods, user input, content displayed on the interface, and nothing more.低耦合
: The decoupling of Model and View determines this feature.
6. What are the advantages of MVP?
- Low coupling: The transformation of Model and View layers will not affect each other.
- Reusability: Model layer can be used for multiple Views. For example, to request video data, multiple pages may need this function, but only one copy of the Model layer code is enough.
方便测试
: You can testModel
layers andView
layers individually .
7. Disadvantages of MVP
MVP
The interface is used to connectview层
andpresenter层
, if there is a page with very complex logic, there will be many interfaces, resulting in a very high cost of maintaining the interface.解决办法
: As far as possible, some common interfaces are used as base classes, and other interfaces are inherited.
8. The realization of MVP?
Model layer
//Model层-数据的实体类:NetInfo.java
public class NetInfo {
private int code;
private String msg;
public NetInfo(int code, String msg){
this.code = code;
this.msg = msg;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
//Model层-请求数据时View和Model的交互接口(中间层Presenter去实现):LoadTasksCallBack.java
public interface LoadTasksCallBack<T> {
void onSuccess(T data);
void onFailed();
}
//Model层-任务抽象基类【业务接口】: NetTask.java
public interface NetTask<T> {
void execute(T data, LoadTasksCallBack callBack);
}
/**=============================================
* Model层核心-具体任务【业务的实际操作】:
* 1. 实现Model层获取数据的操作.
* //NetInfoTask.java
*=============================================*/
public class NetInfoTask implements NetTask<String>{
@Override
public void execute(String ip, LoadTasksCallBack callBack) {
if("192.168.1.1".equals(ip)){
callBack.onSuccess(new NetInfo(1, "This is a Msg from " + ip));
}else{
callBack.onFailed();
}
}
}
Presenter layer
* 契约接口:
* 存放相同业务的Preenter和View-便于查找和维护
* //NetInfoContract.java
*=============================================*/
public interface NetInfoContract{
//1、Presenter定义了获取数据的方法
interface Presenter{
void getNetInfo(String ip);
}
//2、View中定义了与界面交互的方法
interface View extends BaseView<Presenter> {
void setNetInfo(NetInfo netInfo);
void showError(String msg);
}
}
/**=========================================
* Presenter具体实现:NetInfoPresenter.info
* 1. 分别与View层和Model层Task关联起来(持有了两者的对象)
* 2. 实现接口getNetInfo()用于View层从Model层获取数据
* 3. * 次要实现了Task执行中需要的回调接口-代理完成了View与Model的交互(避免了M/V的直接交互)
*========================================*/
public class NetInfoPresenter implements NetInfoContract.Presenter, LoadTasksCallBack<NetInfo>{
//1. View层
private NetInfoContract.View mView;
//2. Model层任务
private NetTask mNetTask;
//3. 分别与View和Model建立关联
public NetInfoPresenter(NetInfoContract.View view, NetTask netTask){
mNetTask = netTask;
mView = view;
}
//4. 能让View层获取到Model层数据
@Override
public void getNetInfo(String ip) {
mNetTask.execute(ip, this);
}
//5. 实现Model层需要的回调接口-作用是将Model层数据交给View层
@Override
public void onSuccess(NetInfo netInfo) {
mView.setNetInfo(netInfo);
}
@Override
public void onFailed() {
mView.showError("error");
}
}
View layer
//BaseView.java
//View层的基类:定义了设置Presenter的接口
public interface BaseView<T> {
void setPresenter(T presenter);
}
//HttpActivity.java
// View层的具体实现:可以是Activity也可以是Fragment
public class HttpActivity extends Activity implements NetInfoContract.View{
//1. 中间代理人
private NetInfoContract.Presenter mPresenter;
Button mGetButton, mSetButton;
TextView mTitleTxtView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity_http);
mGetButton = findViewById(R.id.get_fromnet_button);
mSetButton = findViewById(R.id.set_button);
mTitleTxtView = findViewById(R.id.title_textview);
/**==============================================
* 1、给View层设置Presenter和Model层的Task(获取数据)
*===============================================*/
setPresenter(new NetInfoPresenter(this, new NetInfoTask()));
/**==============================================
* 2、View层通过Presenter去获取数据
*===============================================*/
mGetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO 从网络请求到数据
mPresenter.getNetInfo("192.168.1.1");
}
});
mSetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTitleTxtView.setText("Local Msg = Hello");
}
});
}
/**=====================================
* 3、实现View层的三个接口:设置Presenter和View界面相关功能
*====================================*/
@Override
public void setPresenter(NetInfoContract.Presenter presenter) {
mPresenter = presenter;
}
@Override
public void setNetInfo(NetInfo netInfo) {
mTitleTxtView.setText(netInfo.getMsg());
}
@Override
public void showError(String msg) {
mTitleTxtView.setText(msg);
}
}
public interface LoginMVPContract{
//View接口
public interface ILoginView<T>{
public void showLoginSuccess(T data);
public void showLoginFailed(String errorMsg);
}
//任务接口
public interface ILoginTask{
public void startLogin(String phoneNumber, ILoginCallBack callback);
}
//Presenter
public interface ILoginPresenter{
public void startLogin(String phoneNumber);
}
//Presenter和Task间交互的接口
public interface ILoginCallBack<T>{
public void onLoginSuccess(T data);
public void onLoginFailed(String errorMsg);
}
}
2. Model's LoginResultBean and LoginTask.java
public class LoginResultBean {
}
public class LoginTask implements LoginMVPContract.ILoginTask{
@Override
public void startLogin(String phoneNumber, LoginMVPContract.ILoginCallBack callback) {
if(true){
callback.onLoginSuccess(new LoginResultBean());
}else{
callback.onLoginFailed("登录失败");
}
}
}
3、Presenter
public class LoginPresenter implements LoginMVPContract.ILoginPresenter, LoginMVPContract.ILoginCallBack{
LoginMVPContract.ILoginView mLoginView;
LoginMVPContract.ILoginTask mTask;
public LoginPresenter(LoginMVPContract.ILoginView loginView, LoginMVPContract.ILoginTask task){
mLoginView = loginView;
mTask = task;
}
/**
* 接口回调至
*/
@Override
public void onLoginSuccess(Object data) {
mLoginView.showLoginSuccess(data);
}
@Override
public void onLoginFailed(String errorMsg) {
mLoginView.showLoginFailed(errorMsg);
}
@Override
public void startLogin(String phoneNumber) {
mTask.startLogin(phoneNumber, this);
}
}
4、View
public class LoginFragment extends SupportFragment implements LoginMVPContract.ILoginView<LoginResultBean>{
LoginMVPContract.ILoginPresenter mLoginPresenter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLoginPresenter = new LoginPresenter(this, new LoginTask());
mLoginPresenter.startLogin("17777777777");
}
@Override
public void showLoginSuccess(LoginResultBean data) {
//登陆成功
}
@Override
public void showLoginFailed(String errorMsg) {
//登录失败
}
}
9. How to optimize the amount of MVP files
- Use the
泛型
definition contract class to bemodel、view、presenter
defined in a契约类中
- The structure is clear, one
契约类
corresponds to one业务模块
.
MVVM
10. What are the functions and characteristics of the MVVM model?
Model-View-ViewModel
, Will bePresenter
replaced withViewModel
.ViewModel
AndModel/View
carried out two-way binding.View
When there is a change, itViewModel
will be notifiedModel
to update the dataModel
After the data is updated, the update displayViewModel
will be notifiedView
- Google released a
MVVM
support librariesData Binding
: can bind data toxml
the- Now Google has introduced
ViewModel和LiveData
components for more convenient implementationMVVM
MVVM
Modularization and componentization
11. What is modularity
- A kind
软件设计技术
- The
项目
function is split独立
,可交换
the module- Each
模块
contains the execution单独功能
of必要内容
.
12. What is componentization
- Component software engineering is also called component development, which is a branch of software engineering.
- Emphasize the splitting of a software system into independent components (components can be modules or web resources, etc.)
13. The difference between modularization and componentization
- The purpose of both is
重用和解耦
- mainly
叫法不同
模块化
Focus on reuse,组件化
more on业务解耦
14. Advantages of componentization
- Flexible assembly between components
- A
组件
change, as long as对外提供的接口
there is no change, does其他组件
not need to be tested.- Disadvantages: higher requirements for technology and business understanding.
15. Modular hierarchical split
- Basic library
- General business layer
- Application layer
16. Communication between modules
- You can implement it yourself but it is more troublesome
- Suggested
阿里巴巴
open source library.
After reading the likes, develop a habit, search "Programming Ape Development Center" on WeChat to follow this programmer who likes to write dry goods.
In addition, there is also a complete test site for interviews with major Android first-line companies. The information is updated in my Gitee . Friends who need interviews can refer to them. If it is helpful to you, you can order a Star!
Gitee address : [ Old Chen’s Gitee ]