Android Architecture Pattern – MVC Example

Android Architecture Pattern – MVC Example

MVC stands for Model-View-Controller.

Introduction

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

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

This Post Has 5 Comments

  1. This is a very informative post. Thanks.

  2. Please continue posting.

  3. Do you have videos?

  4. 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

Leave a Reply

Close Menu
%d bloggers like this: