In a world where the user interface logic tends to change more often than the business logic, the desktop and Web developers needed a way of separating user interface functionality. The MVC pattern was their solution.
The model, view, controller approach separates your application at a macro level into 3 sets of responsibilities.
Model
The model is the Data + State + Business logic of our Tic-Tac-Toe application. It’s the brains of our application so to speak. It is not tied to the view or controller, and because of this, it is reusable in many contexts.
View
The view is the Representation of the Model. The view has a responsibility to render the User Interface (UI) and communicate to the controller when the user interacts with the application. In MVC architecture, Views are generally pretty “dumb” in that they have no knowledge of the underlying model and no understanding of state or what to do when a user interacts by clicking a button, typing a value, etc. The idea is that the less they know the more loosely coupled they are to the model and therefore the more flexible they are to change.
Controller
The controller is Glue that ties the app together. It’s the master controller for what happens in the application. When the View tells the controller that a user clicked a button, the controller decides how to interact with the model accordingly. Based on data changing in the model, the controller may decide to update the state of the view as appropriate. In the case of an Android application, the controller is almost always represented by an Activity or Fragment.
There are two Approaches to achieve MVC :
- Passive Model
- 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(); } }