• 20 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 6/21/19

Learn the Model-View-Controller Pattern

Log in or subscribe for free to enjoy all this course has to offer!

In this chapter, we will discuss a common practice of software architecture and apply it to our TopQuiz application.

When you develop an Android application, you are free to apply any software architecture to your project. However, if you make a bizarre choice (or create your own), your code may be difficult for others to understand. If it's too tough to follow, your co-workers will stop talking to you.

That means you'll never know when there's cake in the office – ever. And on your end, you might have a hard time maintaining code written by someone else. So instead of eating delicious cake, you'll be crying tears of loneliness onto your keyboard.

Using a common software architecture makes code maintenance a piece of cake!
Using a common architecture makes code a piece of cake!

Most Android developers use a common architecture called MVC, or Model-View-Controller. This pattern is classic, and you will find it in the majority of development projects. It's not the only software pattern, but it's the one we'll study in this course and apply to our TopQuiz application.

MVC Pattern

The MVC pattern splits code into one of three MVC components. When you create a new class or file, you must know which component it belongs to:

  • Model: contains application data and business logic (the rules of the system). For example, user accounts, products you sell, a set of photos, etc. The model component has no knowledge of the interface. In TopQuiz, it will store questions and their answers.

  • View: contains everything that is visible on the screen and offers interaction to the user. In TopQuiz, this component is defined in the activity_main.xml and activity_game.xml files.

  • Controller: This is the "glue" between the view and the model, which also manages the application logic. The controller reacts to the user's input and presents the data requested by the user. Where does it retrieve the data from? Yes, you guessed it: the model. In TopQuiz, we have two controllers: the MainActivity and GameActivity classes.

To make your code more modular and maintainable (your future colleagues will thank you, with cake), create 3 additional packages in Android Studio. Name them "Model," "View," and "Controller."

How do I create a subdirectory to organize my code?

Right-click the folder where you want to create a subdirectory and select New > Package. Name it something descriptive, like "controller." Move files to this new directory (e.g. Activity files), and validate by clicking the Refactor button. Android Studio will ask you to confirm your choice: click Do Refactor.

Application

When the user interacts with the application - for example, by answering a question - this is what happens:

MVC in action
MVC in action

At step 3, the controller can update the model, if necessary - for example, if the user updates their application preferences.

Advantages

By applying the MVC pattern, you allow the majority of Android developers to readily understand your code. You also make it easier to reuse your code later. For example, you can export your application template as a library so that another developer can use it in their application or so that you can use it in your next app.

Disadvantages

You will have to create and maintain more files. I know, it's quite tempting to put everything in the same place, but this necessary evil is for everyone's benefit.

Let's recap!

This may have been your first exposure to the MVC pattern, and hopefully it was a good one! Moving forward, keep the following in mind:

  • Using the MVC model provides an organization for your code and makes it easier for others to read or modify it. 

  • The MVC model is made up of three components: 

    • Model: contains data and business logic and interacts with the controller. 

    • View: contains what is visible to the user (or allows the user to interact with elements on the screen).

    • Controller: interacts with the view and the model by responding to user input and retrieving data the user requests. 

  • Organize your files and classes into additional packages (in subdirectories) that correspond to either the Model, the View, or the Controller. 

When you create a file, always ask yourself: What component of the MVC pattern does it represent? If you can't decide, you may need to refactor your code.

We'll look more in depth at each of the MVC components in the following chapters.

Example of certificate of achievement
Example of certificate of achievement