LogoLogo
6.1.1
6.1.1
  • TotalCross Overview
  • TotalCross Javadoc
  • TotalCross Changelog
  • Roadmap
  • Documentation
    • Getting Started
      • First embedded project with TotalCross
    • Components
      • Accordion
      • Aligned Labels
      • Button
      • Check
      • ComboBox
      • Dynamic Scroll
      • Edit
      • Floating Button
      • Gpiod
      • Grid
      • GridContainer
      • Image
      • ImageControl
      • ImageList
      • Label
      • Material Icons
      • Material Window
      • MessageBox
      • Multi Edit
      • Progress Bar
      • Progress Box
      • Radio
      • Radio Group
      • Scroll Container
      • Side Menu
      • Slider
      • Sliding Window
      • Spin List
      • Spinner
      • Switch
      • Tabbed Container
      • Velocimeter
    • APIs
      • API Overview
      • API Rest
      • Asynchronous Task
      • Camera
      • Control
        • Main Window
        • Window
        • Container
      • GPS
      • HTTPS and SSL
      • JSON
      • Maps
        • Maps - Deprecated
        • Static Map
      • Material Design Standards
      • Ninepath
      • Notifications
      • PrinterManager
      • Push Notification Firebase
      • Scanner
      • SOAP
      • Socket
      • SocketServer
      • SQLite Encryption
      • QR Code Generator
      • totalcross.sys
      • Youtube API
    • Creating an Issue
    • Contributing
      • Branch workflow
      • Writing documentation
    • Guides
      • App Architecture
        • Suggested Architecture
        • Why do Design Patterns help with the application's organization?
          • MVC Architecture Pattern
          • Template Pattern
          • Data Persistence: DAO Pattern.
        • Separation of concepts: What is the best way to create UI interfaces?
        • Positioning
          • Manual Positioning
        • Relative Positioning
        • Best practices to improve project maintenance
      • Device Simulator
      • Package your app from scratch
        • TotalCross SDK
        • Environment Variables in IDE
          • Eclipse
          • IntelliJ
        • Deploy your app with a dependecy TC
        • Deploy iOS
          • Using Development certificate to test your apps
      • Understanding TotalCross for Linux ARM
      • Running C++ applications with TotalCross
      • Web Services
    • Miscelaneous
      • Java JDK 8
      • Maven
      • Installing Visual Studio Code
    • FAQ
      • IMEI in Android 10
Powered by GitBook
On this page
  • Structures
  • Referencies

Was this helpful?

  1. Documentation
  2. Guides
  3. App Architecture
  4. Why do Design Patterns help with the application's organization?

MVC Architecture Pattern

This architecture pattern will assign and separate the objects into responsibilities: Model, View and Controller

  • Model: will represent an object, such as a person.

  • View: The view will represent the model data.

  • Controller: It controls the data of the model object and controls the visualization.

Structures

Structures
└── src
    └── main
        └── java
            └── com.your_company_name.your_name_app
                └── model
                    └── Person
                └── view
                    └── HomeView
                └── controller
                    └── HomePresenter
package com.totalcross.mvc.model;

public class Person {
    private String name;
    private String gender;

    public Person(String name, String gender) {
        this.name = name;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "n: " + name + " g: " + gender;
    }
}
package com.totalcross.mvc.view;

import com.totalcross.mvc.controller.HomePresenter;
import com.totalcross.mvc.model.Person;
import totalcross.ui.Button;
import totalcross.ui.Edit;
import totalcross.ui.Label;
import totalcross.ui.MainWindow;

public class HomeScreen extends MainWindow {

   //Create a variable to store the interface of the class.
    Presenter presenter;

    Person person [] = new Person[5];
    private int gap = 15;

/*
The constructor will be responsible for instantiating the HomePresenter class 
which will link the presenter of the interface with the controller
*/
    public HomeScreen(){
        new HomePresenter(this);
    }

    @Override
    public void initUI() {

        Label lbl = new Label("Person");
        add(lbl, CENTER, TOP + gap);

        Edit editName = new Edit();
        editName.caption = "Insert your name here";
        add(editName, LEFT, AFTER + gap);

        Edit editGender = new Edit();
        editGender.caption = "Insert your gender here";
        add(editGender, LEFT, AFTER + gap);

        Button btn = new Button("Create Person");
        //Assigning the onCreate method to the btn button
        btn.addPressListener( e -> presenter.onCreate(person, editName, editGender));
        add(btn, RIGHT, AFTER + gap);
    }

    //Creating the interface and the methods that will be used on this screen
    public interface Presenter{
        void onCreate(Person[] person, Edit name, Edit gender);
    }

    //Method that allows you to add the Controller Presenter in the view
    public void addListener(Presenter presenter) {
        this.presenter = presenter;
    }


}

package com.totalcross.mvc.controller;

import com.totalcross.mvc.model.Person;
import com.totalcross.mvc.view.HomeScreen;
import totalcross.ui.Edit;
import totalcross.ui.Toast;

public class HomePresenter implements HomeScreen.Presenter {
/*
    In the constructor the Presenter link of the controller is realized 
    with the view.
*/
    public HomePresenter(HomeScreen home){
        home.addListener(this);
    }
    
    //The method that will be used on the button is implemented.
    @Override
    public void onCreate(Person[] person, Edit name, Edit gender) {
        for (int i = 0; i < person.length; i++) {
            if (person[i] == null){
                person[i] = new Person(name.getText(), gender.getText());
                break;
            }
        }

        String persons = "";
        for (Person p: person) {
            persons += p + " / ";
        }
        Toast.show(persons,2000);
        System.out.println(persons);
    }
}

Referencies

PreviousWhy do Design Patterns help with the application's organization?NextTemplate Pattern

Last updated 6 years ago

Was this helpful?

See more about MVC in .

Geeks for Geeks