Android architecture design - MVC

objective

The purpose of architecture design is to modularize the program through design, so as to achieve high aggregation within the module and low coupling between modules. Improve development efficiency and facilitate subsequent testing and problem location. However, for apps with different orders of magnitude and different requirements, the appropriate architecture depends on the situation, and the architecture must not be rigid.

definition

MVC is the abbreviation of Model View Controller. It refers to the design architecture of Model View Controller. Of which:

  • Model (model layer): responsible for loading and storing data

  • View: it is responsible for displaying interface data and interacting with users

  • Controller (controller layer): responsible for processing logical business

technological process

objective

The purpose of architecture design is to modularize the program through design, so as to achieve high aggregation within the module and low coupling between modules. Improve development efficiency and facilitate subsequent testing and problem location. However, for apps with different orders of magnitude and different requirements, the appropriate architecture depends on the situation, and the architecture must not be rigid.

definition

MVC is the abbreviation of Model View Controller. It refers to the design architecture of Model View Controller. Of which:

  • Model (model layer): responsible for loading and storing data

  • View: it is responsible for displaying interface data and interacting with users

  • Controller (controller layer): responsible for processing logical business

technological process

  • View accepts the user's request and passes the request to the Controller.
  • After the Controller performs business logic processing, it notifies the Model to update.
  • After the Model data is updated, notify the View to update the interface display.

MVC in Android

The MVC framework is also used in the interface part of Android: generally, the Activity acts as the Controller, the XML file acts as the View layer, and the Model layer we extracted.

View layer

XML files are generally used to describe the interface. These XML files can be understood as the View of android app. When in use, it can be introduced very conveniently, and it is convenient for later interface modification. If the id corresponding to the interface in the logic does not change, the code does not need to be modified, which greatly enhances the maintainability of the code.

Control layer (Controller)

The responsibility of Android's control layer usually falls on the shoulders of many activities. The response time of Activity in Android is 5s. If time-consuming operations are put here, the program can be easily recycled. Therefore, time-consuming operation codes are generally not written in the Activity, but processed through the Activity delivery Model business logic layer.

Model layer

The data structure and related classes established for the business Model can be understood as the Model of AndroidApp. The Model is not related to View, but related to business. Database operations and network operations should be handled in the Model. Of course, business calculations and other operations must also be placed in this layer.

Content of each layer

Android MVC small example

Let's look at a simple demo code. Its function is to give the running time of the application after clicking the refresh button on the screen.

code

//Model layer
public class TimeModel {
    private static final long START_TIME = System.currentTimeMillis();
    public void update(ControllerActivity controllerActivity) {
        //Update data
        String timeText = Double.toString((System.currentTimeMillis() - START_TIME)/1000.0);
        //Update UI
        controllerActivity.setText(timeText);
    }
}
<!-- View layer -->
<LinearLayout xmlns:android=
    "http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="@string/time_text"/>

    <Button
        android:id="@+id/btn_update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/update_button"/>
</LinearLayout>
//Controller layer
public class ControllerActivity extends Activity {
    private TimeModel timeModel;
    private TextView textView;
    private Button button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_controller);
        timeModel = new TimeModel();
        initView();
    }

    public void initView() {
        textView = findViewById(R.id.tv_show);
        button = findViewById(R.id.btn_update);
        //Receive events from View
        button.setOnClickListener(v -> {
            //Notify Model to process data
            timeModel.update(ControllerActivity.this);
        });
    }

    public void setText(String timeText){
        String text = "App already running" + timeText + "second";
        textView.setText(text);
    }
}

Operation results

shortcoming

The responsibilities of each component in the above example seem to be in line with the MVC design concept, but in the actual development process, we find that this is not the case.

The XML View function as View in Android is too weak. Activity can basically be regarded as the combination of View and Controller. It is not only responsible for the display of View, but also adds control logic, resulting in the coupling between Controller layer and View layer. MVC written in Android is more similar to View model mode:

Therefore, using MVC architecture in Android development does have many constraints. It is a general trend to find a more suitable design architecture.

last

When Xiaobian learned to improve, he collected and sorted out some learning documents, interview questions, Android core notes and other documents related to Android development from the Internet, hoping to help you learn and improve. If you need reference, you can go to me directly CodeChina address: https://codechina.csdn.net/u012165769/Android-T3 Access.

Keywords: Android Design Pattern architecture

Added by luv2sd on Fri, 31 Dec 2021 15:15:39 +0200