Skip to content

Exercice MVC

Objectif de l’exercice

Développer une application Java de gestion de tâches utilisant :

  • Modèle MVC
  • Injection de dépendances
  • Interface de vue
  • Deux vues console interchangeables :
    • ConsoleView (simple)
    • ColoredConsoleView (avec couleurs ANSI)

Architecture du projet

src/
 └── app/
      ├── model/
      ├── view/
      ├── controller/
      └── Main.java
Architecture MVC + DI - Gestion de tâchesArchitecture MVC + DI - Gestion de tâchesappmodelviewcontrollerTasktitle : Stringdescription : Stringcompleted : booleanTask(title : String, description : String)markCompleted() : voidisCompleted() : booleangetTitle() : StringtoString() : StringTaskRepositorytasks : List<Task>add(task : Task) : voidgetAll() : List<Task>findByTitle(title : String) : TaskmarkCompleted(title : String) : voidViewafficherMessage(msg : String) : voidafficherTaches(tasks : List<Task>) : voidafficherTache(task : Task) : voidConsoleViewafficherMessage(msg : String) : voidafficherTaches(tasks : List<Task>) : voidafficherTache(task : Task) : voidColoredConsoleViewRESET : StringRED : StringGREEN : StringYELLOW : StringCYAN : StringBOLD : StringafficherMessage(msg : String) : voidafficherTaches(tasks : List<Task>) : voidafficherTache(task : Task) : voidTaskControllerrepo : TaskRepositoryview : ViewTaskController(repo : TaskRepository, view : View)addTask(title : String, desc : String) : voidlistTasks() : voidfindTask(title : String) : voidcompleteTask(title : String) : voidMainmain(args : String[]) : voidcontient1*utiliseutilisecréecréeinjecte les dépendances
Architecture MVC + DI - Gestion de tâchesArchitecture MVC + DI - Gestion de tâchesappmodelviewcontrollerTasktitle : Stringdescription : Stringcompleted : booleanTask(title : String, description : String)markCompleted() : voidisCompleted() : booleangetTitle() : StringtoString() : StringTaskRepositorytasks : List<Task>add(task : Task) : voidgetAll() : List<Task>findByTitle(title : String) : TaskmarkCompleted(title : String) : voidViewafficherMessage(msg : String) : voidafficherTaches(tasks : List<Task>) : voidafficherTache(task : Task) : voidConsoleViewafficherMessage(msg : String) : voidafficherTaches(tasks : List<Task>) : voidafficherTache(task : Task) : voidColoredConsoleViewRESET : StringRED : StringGREEN : StringYELLOW : StringCYAN : StringBOLD : StringafficherMessage(msg : String) : voidafficherTaches(tasks : List<Task>) : voidafficherTache(task : Task) : voidTaskControllerrepo : TaskRepositoryview : ViewTaskController(repo : TaskRepository, view : View)addTask(title : String, desc : String) : voidlistTasks() : voidfindTask(title : String) : voidcompleteTask(title : String) : voidMainmain(args : String[]) : voidcontient1*utiliseutilisecréecréeinjecte les dépendances

Modèle

Task.java


TaskRepository.java

package app.model;

import java.util.ArrayList;
import java.util.List;

public class TaskRepository {
    private final List<Task> tasks = new ArrayList<>();
}

Interface de vue

View.java



Vue 1 : ConsoleView (simple)

ConsoleView.java



Vue 2 : ColoredConsoleView

ColoredConsoleView.java

package app.view;

import app.model.Task;
import java.util.List;

public class ColoredConsoleView implements View {
    private static final String RESET = "\u001B[0m";
    private static final String RED = "\u001B[31m";
    private static final String GREEN = "\u001B[32m";
    private static final String YELLOW = "\u001B[33m";
    private static final String CYAN = "\u001B[36m";
    private static final String BOLD = "\u001B[1m";
}

Contrôleur

TaskController.java



Main.java (test des deux vues console)

import app.controller.TaskController;
import app.model.TaskRepository;
import app.view.ConsoleView;
import app.view.ColoredConsoleView;
import app.view.View;

public class Main {
    public static void main(String[] args) {

        TaskRepository repo = new TaskRepository();

        // Vue console simple :
        // View view = new ConsoleView();

        // Vue console colorée :
        View view = new ColoredConsoleView();

        TaskController controller = new TaskController(repo, view);

        controller.addTask("Étudier MVC", "Lire le chapitre 5");
        controller.addTask("Faire l'exercice DI", "Créer deux vues console");
        controller.listTasks();
        controller.completeTask("Étudier MVC");
        controller.findTask("Étudier MVC");
    }
}