Skip to content

Modèle MVC en Java

Introduction générale au modèle MVC

Le modèle MVC (Modèle–Vue–Contrôleur) est un patron d’architecture structurant les applications en séparant trois responsabilités : - le modèle (logique et données), - la vue (interface ou sortie), - et le contrôleur (coordination des actions).

Rôles centraux

  • Modèle : gère les données et la logique métier.
  • Vue : affiche les informations sans logique métier.
  • Contrôleur : reçoit les actions, modifie le modèle et déclenche les mises à jour.

Structure MVC en Java

Exemple recommandé d’arborescence :

src/
 └── app/
      ├── model/
      ├── view/
      └── controller/
      └── Main.java

Exemple complet : Gestion d’une bibliothèque

Modèle

Livre.java

package app.model;

public class Livre {
    private String titre;
    private String auteur;
    private int annee;

    public Livre(String titre, String auteur, int annee) {
        this.titre = titre;
        this.auteur = auteur;
        this.annee = annee;
    }

    public String getTitre() { return titre; }
    public String getAuteur() { return auteur; }
    public int getAnnee() { return annee; }

    @Override
    public String toString() {
        return titre + " (" + auteur + ", " + annee + ")";
    }
}

Bibliotheque.java

package app.model;

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

public class Bibliotheque {
    private List<Livre> livres = new ArrayList<>();

    public void ajouterLivre(Livre livre) {
        livres.add(livre);
    }

    public List<Livre> getLivres() {
        return livres;
    }

    public Livre chercherParTitre(String titre) {
        return livres.stream()
                .filter(l -> l.getTitre().equalsIgnoreCase(titre))
                .findFirst()
                .orElse(null);
    }
}


Vue

ConsoleView.java

package app.view;

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

public class ConsoleView {
    public void afficherMessage(String message) {
        System.out.println(message);
    }

    public void afficherLivres(List<Livre> livres) {
        if (livres.isEmpty()) {
            System.out.println("Aucun livre disponible.");
        } else {
            System.out.println("Liste des livres :");
            livres.forEach(l -> System.out.println("- " + l));
        }
    }

    public void afficherLivreTrouve(Livre livre) {
        if (livre == null) {
            System.out.println("Livre non trouvé.");
        } else {
            System.out.println("Livre trouvé : " + livre);
        }
    }
}


Contrôleur

BibliothequeController.java

package app.controller;

import app.model.Bibliotheque;
import app.model.Livre;
import app.view.ConsoleView;

public class BibliothequeController {
    private Bibliotheque model;
    private ConsoleView view;

    public BibliothequeController(Bibliotheque model, ConsoleView view) {
        this.model = model;
        this.view = view;
    }

    public void ajouterLivre(String titre, String auteur, int annee) {
        model.ajouterLivre(new Livre(titre, auteur, annee));
        view.afficherMessage("Livre ajouté avec succès.");
    }

    public void afficherLivres() {
        view.afficherLivres(model.getLivres());
    }

    public void chercherLivre(String titre) {
        Livre l = model.chercherParTitre(titre);
        view.afficherLivreTrouve(l);
    }
}


Programme principal

Main.java

package app;

import app.controller.BibliothequeController;
import app.model.Bibliotheque;
import app.view.ConsoleView;

public class Main {
    public static void main(String[] args) {
        Bibliotheque model = new Bibliotheque();
        ConsoleView view = new ConsoleView();
        BibliothequeController controller = new BibliothequeController(model, view);

        controller.ajouterLivre("1984", "George Orwell", 1949);
        controller.ajouterLivre("Dune", "Frank Herbert", 1965);

        controller.afficherLivres();
        controller.chercherLivre("Dune");
        controller.chercherLivre("Inconnu");
    }
}

Conclusion

Le modèle MVC permet de structurer proprement les applications Java, d'améliorer la maintenabilité et de faciliter la création d'applications robustes.