Android architecture design - MVC, memory optimization for Android interview questions

Control layer (Controller)

The heavy 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 placed here, the program can be easily recycled. Therefore, it is generally not necessary to write time-consuming operation codes in the Activity, but to deliver the Model business logic layer processing through the Activity.

Model layer (Model)

The data structure and related classes we established for the business model can be understood as the Model of the Android App. The Model is not related to the View, but related to the business. The operations on the database and the network should be handled in the Model. Of course, operations such as business computing must also be placed in this layer.

Layer content

Android MVC small example


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

the 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 the UI

controllerActivity.setText(timeText);

}

}

<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"/>

//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 = "Application is running" + timeText + "seconds";

textView.setText(text);

}

}

operation result

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 found that this is not the case.

at last

If you read this and think the article is well written, give it a thumbs up? If you think there is something worth improving, please leave me a message. I will definitely check carefully and correct the deficiencies. Thanks.

Finally, put a bonus at the end of the article: GitHub address

PS: I have a lot of high-level Android learning video materials and interview data packages in my GitHub~

b/master/Android%E5%BC%80%E5%8F%91%E4%B8%8D%E4%BC%9A%E8%BF%99%E4%BA%9B%EF%BC%9F%E5%A6%82%E4%BD%95%E9%9D%A2%E8%AF%95%E6%8B%BF%E9%AB%98%E8%96%AA%EF%BC%81.md)**

PS: I have a lot of high-level Android learning video materials and interview data packages in my GitHub~

Welcome everyone to exchange and discuss together~

Guess you like

Origin blog.csdn.net/m0_66264630/article/details/122964387