I'm new at MVC pattern and Android development.
What I did is a simple app which let the user to tap on a button called "Increment" to increment a number's value and another button, called "Reset", which allows to reset that number to 0 if number % 5 == 0
.
What I did is:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonAzzera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_azzera"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.885"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<Button
android:id="@+id/buttonIncrementa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_incrementa"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.148"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/buttonAzzera"
app:layout_constraintStart_toEndOf="@+id/buttonIncrementa"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.provamvc;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button buttonReset;
Button buttonIncrement;
TextView numberLabel;
Model model = new Model();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonReset = findViewById(R.id.buttonAzzera);
buttonIncrement = findViewById(R.id.buttonIncrementa);
numberLabel = findViewById(R.id.textView);
buttonIncrement.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
model.incrementNumber();
numberLabel.setText(String.valueOf(model.getNumber()));
if (model.isNumberResettable())
buttonReset.setEnabled(true);
else
buttonReset.setEnabled(false);
}
});
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
model.resetNumber();
numberLabel.setText("0");
buttonReset.setEnabled(false);
}
});
}
}
and finally Model.java
package com.example.provamvc;
public class Model {
private int number;
public Model() {
number = 0;
}
public int getNumber() {
return number;
}
public boolean isNumberResettable() {
return number % 5 == 0;
}
public void incrementNumber() {
number++;
}
public void resetNumber() {
if (isNumberResettable()) {
number = 0;
}
}
}
This works fine.
- My question is: does
MainActivity.java
represent theController
(of the whole application) and does theactivity_main.xml
represent theView
? Or should I create another class which will be aController
? - Is this tiny application designed well or it violates the MVC Design Pattern and the principle of "high coesion and low low coupling"?
- Should I use Observer-Observable pattern too for better respecting the MVC pattern?
Aucun commentaire:
Enregistrer un commentaire