You are currently viewing Android Architecture Pattern – MVC

Android Architecture Pattern – MVC

  • Post author:
  • Post category:Android
  • Post comments:13 Comments
  • Post last modified:December 10, 2021
  • Reading time:5 mins read

Android Architecture Pattern – MVC stands for Model-View-Controller. While developing big application Architecture Patterns are the right solutions.

    • Model — the data layer, responsible for managing the business logic and handling network or database API.
    • View — the UI layer — a visualisation of the data from the Model.
    • Controller — the logic layer, gets notified of the user’s behavior and updates the Model as needed.

Reference

Example

Hello everyone. Let’s go for a simple, very basic example of MVC in android. But before that let’s know together what that so called MVC with example.

Designing MVC does not means that we should have three separate classes for model, view and controller. We can create controller in the view section as well.

But for simple case we are gonna create three different classes

  1. Model => Student Model
  2. Controller => StudentController
  3. View => MainActivity

There are two Approaches to achieve MVC :

  1. Passive Model
  2. Active Model

Passive Model

In this concept, Controller is the only class can control the model and modify the view. That means model is connected through controller with the model.

Active Model

In this concept, Controller is not the only class who can control the model. Model can be modify by other classes. It just need some help to notify the view. This can be done by the observer which is available in the java.util package.

In this example we will be working on the passive model.

Student.java

Firstly create student model class.

public class Student  {

  private static final String TAG = "Student";

  private String rollNo;
  private String name;

  public String getRollNo() {
    return rollNo;
  }

  public void setRollNo(String rollNo) {
    this.rollNo = rollNo;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

We have created the Student Model class which have two private variables name,rollNo. Then added the getter and setter for those two variables.

All the business logic’s should be implemented in the model

We can set data by setName() and fetch the data using getName();

StudentController.java

Secondly we create StudentController.

public class StudentController {
  
  private Student model;
  private MainActivity view;

  public StudentController(Student model, MainActivity view) {
    this.model = model;
    this.view = view;
  }

  public void setStudentName(String name){
    model.setName(name);
  }
  public String getStudentName(){
    return model.getName();
  }

  public void setStudentRoll(String roll){
    model.setRollNo(roll);
  }

  public String getStudentRoll(){
    return model.getRollNo();
  }
  
  public void updateView(){
    view.printStudentDetails(model.getName(),model.getRollNo());
  }
}

Here we can see that we have created constructor of the this controller which gets two parameters. One is the model and the another one is the view. As the view we are getting MainActivity. we can get data from the database of from the view and set the data to the model with the help of setStudentName() and setStudentRoll() method. The other two get methods will return the values from the model. This is one of the examples of Encapsulation. 

There is another method updateView(). What is does is it calls one of the method from the view. In this case we called the printStudentDetails() method which takes two parameters.

public void printStudentDetails(String studentName, String studentRollNo){

    Log.d(TAG, "printStudentDetails: "+"Name: " + studentName);
    Log.d(TAG, "printStudentDetails: "+"Roll No: " + studentRollNo);

  }

And this method is responsible for the UI update. In this case we are logging the data. Now let’s see the MainActivity.java class.

MainActivity.java

Thirdly create MainActivity.java as a view.

public class MainActivity extends AppCompatActivity {

  private String TAG = "MVCLog";

  public StudentController controller;
  public TextView tvName,tvRoll;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Student model = retriveStudetnFromDatabase();
    MainActivity view = new MainActivity();

    controller = new StudentController(model,view);
    controller.updateView();

  }

  private Student retriveStudetnFromDatabase() {

    Student student = new Student();
    student.setName("Robert");
    student.setRollNo("10");
    return student;
  }

  public void printStudentDetails(String studentName, String studentRollNo){

    Log.d(TAG, "printStudentDetails: "+"Name: " + studentName);
    Log.d(TAG, "printStudentDetails: "+"Roll No: " + studentRollNo);

  }

  public void btnUpdateUI(View view) {

    // update model data
    controller.setStudentName("Rusho");
    controller.setStudentRoll("12");
    controller.updateView();

  }
}

Look at the code and start tweaking with the codes. We will get better knowledge on the MVC pattern.

We can see the source code from the Github.

In conclusion

So now we have some knowledge about mvc in android.

Keep in touch with androvaid.comIn the next chapter we will be discussing about MVP and MVVM.

This Post Has 13 Comments

  1. yidebihapi

    This is a very informative post. Thanks.

  2. dreamcatcher

    Please continue posting.

  3. koyu

    Do you have videos?

    1. Sadman

      Not yet but i will start very soon. Thanks for asking @koyu

  4. Mohammed

    Thank you very much happyMan😉😉
    I searched a lot to find an example of MVC pattern in android , Finally I reached a very clear example So thanks again

    1. Sadman

      You are welcome Sir. 🙂

  5. Kali Junior Brown

    I really love this post. It was so helpful. Thanks man.

  6. Vlarislav

    What for to set the variable view, as you can set this activity yo the constror of the controller.
    Like
    StudentController(model, this) ;
    And what for this MVC? As the view knows about model. Thought, that only controller should know about model, so we shouldn’t set the model to the constructor, like
    StudentController(this)
    And there we should make something like this:
    public StudentVontroller(Activity activity) {
    this. activity = activity;
    This. model = new StudentModel() ;
    }

  7. atakan

    Thanks a lot

  8. Zeus

    As far as i have read the documentation there is no interaction between model and view directly,
    it is to be done via controller, how come u are creating model class object inside view class?.
    if wrong plz solve!.

  9. NASA

    MainActivity view = new MainActivity();
    what?
    this must be MainActivity view = this;
    ???

Leave a Reply