2

I can't get my head around how to apply the model-view-controller design pattern in an android studio project.

I understand that the view is the .xml layouts and that the activity classes are supposed to be the controllers, but I'm confused about whether general programming logic should be a part of the controller or model.

Can the programming logic related to a particular activity be included within the activity class or should they be separated into two classes? i.e. the activity handling just user input and a second class dealing with the associated programming logic.

2
  • Usually, if you have a lot of logic, you can write it into another class, and call it into your activity class where other activities can access as well, if the code is minimal, you can actually put it in the activity, but if other activity need to access it, it is not recommended. Commented Aug 30, 2018 at 1:27
  • Makes sense, thanks. Commented Aug 30, 2018 at 2:21

4 Answers 4

4

It's always a good idea to isolate your code depending on the responsibilities. It's gives us several advantages when it's come to code readability and maintainability.

Since you have asked for MVC pattern but here we have a very good example of MVP pattern which is explained on the following link of Google samples regarding clean architecture in Android projects.

Link: https://github.com/googlesamples/android-architecture/blob/todo-mvp/README.md

enter image description here

Components:

Model: More of your business entities/Pojo Views: More of your fragments and activities Repository: Data source for the information. It can be one of your database, cache, remote server, file system etc. Presentor: It's a layer which get data from one of your repository and send result back to your views.

I have tried to implement similar pattern in one of my learning project.

Link: https://github.com/amol-kamble/movie-finder/tree/master/app/src/main

Sign up to request clarification or add additional context in comments.

1 Comment

Well justified answer. This is what I was looking for.
2

hello @Somebloke It is as simple as its name.

  1. Android Project Structure means the necessary file that are using to developed an Android Application. for example src file, gen file etc.
    • src: This folder contains the Java source files
    • gen: Generated Java library, this library is for Android internal use only.
    • Res: Here we can store resource files such as pictures, XML files for defining layouts, and so forth. Within this folder there are additional folders such as Drawable, Layout, and Values.
    • AndroidManifest.xml: This is the Android definition file. It contains information about the Android application such as minimum Android version, permission to access Android device capabilities such as INTERNET access permission, ability to use phone permission, etc.

2.MVC :- It is one of the structure that are used developed an Android Application.

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

you can take this one as an example https://github.com/hkusu/android-mvc-sample

1 Comment

What's the difference between "business logic" (which goes in the model) from logic that goes in the controller?
0

An architecture decision could be:

  • Whether the application is expected to function online or offline?
  • When packaging an application, how should you distribute top-level packages in the codebase?
  • Dependency injection.
  • Unit testing.

Architecture is our attempt to manage risks involved in changing requirements. As with any other risk-management activity, we cannot be prepared for all the possible risk that can come into play. We need to choose a subset of possible future changes that we are optimizing our application for, and this subset will constitute the application architecture.

Here is a knowledge base to get started, you should make your own project and implement these software architectures in order to adopt the one that works for you and your project:

Android Architecture Blueprints

Android Architecture Blueprints

Reactive Apps with MODEL-VIEW-INTENT - PART1 - Model

enter image description here

Comments

0

//Student Model Class
public class 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;
   }
}

//Student View Class
public class StudentView {

  private TextView name;
  private TextView rollNo;



   public StudentView(View view){
      name=(TextView)view.findViewById(R.id.tv_name);
      rollNo=(TextView)view.findViewById(R.id.tv_roll_no);
   }


   public void setStudentDetails(Student studentModel){
      name.setText(studentModel.getName());
      rollNo.setText(studentModel.getRollNo());
   }
}

//Student Controller Class
public class StudentController {
   private Student model;
   private StudentView view;

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

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

   public String getStudentName(){
      return model.getName();		
   }

   public void setStudentRollNo(String rollNo){
      model.setRollNo(rollNo);		
   }

   public String getStudentRollNo(){
      return model.getRollNo();		
   }

   public void updateView(){				
      view.setStudentDetails(model);
   }	
}

You can use the MVC pattern as given above.Create objects of all three classes in activity and pass the activity's view(xml file) reference to the StudentView object.Create child views in StudentView class as mentioned in xml file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.