Andori architecture high-level interview questions-MVC, MVP, MVVM, componentization, modular analysis

MVC

1. What is MVC in Android? Features?

  1. Model: Data structure and classes established for the business model (not related to View, only related to business)
  2. View: XML/JAVAOr JS+HTMLdisplay the page. Activity/FrgamentAlso assumes the function of View.
  3. Controller: AndroidThe control layer is usually in it Activity、Fragment.
    The essence is the data of the Controlleroperation Modellayer, and it will be 数据returned to the Viewlayer for display.

2. The disadvantages of Android MVC:

  1. ActivityNot MVCin the standard Controller, both Controllerduties have Viewresponsibilities, resulting in Activitycode that is too bloated.
  2. View层And Model层coupled to each other 耦合过重, 代码量过大and difficult to develop and maintain.

MVP

3. MVP mode in Android

  1. MVP(Model-View-Presenter)
  2. Model: Mainly provide data storage function. PresenterNeed to Modelaccess data.
  3. View: Responsible for processing 点击事件和视图展示( Activity、Fragment或者某个View控件)
  4. PresenterView和ModelThe bridge between, Modelafter retrieving the data, returning to the Viewlayer. So that M/Vthere is no longer a coupling relationship.

4. What is the difference between MVP and MVC? (2)

  1. MVPViewDirect access is never allowed inModel
  2. The essence is to 增加了一个接口lower one level耦合度

5. Features of MVP

  1. PresenterCompletely Modeland Viewdecoupled, the main logic is Presenterin.
  2. PresenterIt is 具体Viewnot directly related to each other, 接口and interacts through a well-defined one .
  3. ViewWhen changed, it can remain Presenterunchanged (in line with the characteristics of object-oriented programming)
  4. ViewThere should only be simple Set/Getmethods, user input, content displayed on the interface, and nothing more.
  5. 低耦合: The decoupling of Model and View determines this feature.

6. What are the advantages of MVP?

  1. Low coupling: The transformation of Model and View layers will not affect each other.
  2. 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.
  3. 方便测试: You can test Modellayers and Viewlayers individually .

7. Disadvantages of MVP

  1. MVPThe interface is used to connect view层and presenter层, if there is a page with very complex logic, there will be many interfaces, resulting in a very high cost of maintaining the interface.
  2. 解决办法: 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

  1. Use the 泛型definition contract class to be model、view、presenterdefined in a契约类中
  2. The structure is clear, one 契约类corresponds to one 业务模块.

MVVM

10. What are the functions and characteristics of the MVVM model?

  1. Model-View-ViewModel, Will be Presenterreplaced with ViewModel.
  2. ViewModelAnd Model/Viewcarried out two-way binding.
  3. ViewWhen there is a change, it ViewModelwill be notified Modelto update the data
  4. ModelAfter the data is updated, the update display ViewModelwill be notifiedView
  5. Google released a MVVMsupport libraries Data Binding: can bind data to xmlthe
  6. Now Google has introduced ViewModel和LiveDatacomponents for more convenient implementationMVVM

MVVM

Modularization and componentization

11. What is modularity

  1. A kind软件设计技术
  2. The 项目function is split 独立, 可交换the module
  3. Each 模块contains the execution 单独功能of 必要内容.

12. What is componentization

  1. Component software engineering is also called component development, which is a branch of software engineering.
  2. 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

  1. The purpose of both is重用和解耦
  2. mainly叫法不同
  3. 模块化Focus on reuse, 组件化more on业务解耦

14. Advantages of componentization

  1. Flexible assembly between components
  2. A 组件change, as long as 对外提供的接口there is no change, does 其他组件not need to be tested.
  3. Disadvantages: higher requirements for technology and business understanding.

15. Modular hierarchical split

  1. Basic library
  2. General business layer
  3. Application layer

16. Communication between modules

  1. You can implement it yourself but it is more troublesome
  2. 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 ]

Guess you like

Origin blog.csdn.net/qq_39477770/article/details/108868865