Exercice: Vos premières associtations
Dans cet exercice, vous devez modéliser le catalogue Netflix en mettant l’accent sur les relations entre les Séries, leurs Épisodes et leurs Genres. L’objectif est de vous familiariser avec les concepts d’agrégation, de composition et d’encapsulation en programmation orientée objet. Pour cela, vous allez créer des classes représentant ces éléments et leurs interactions.
Commencez par définir les classes Série, Genre et Épisode, en incluant les attributs et méthodes mentionnés dans le diagramme de classe fourni. La classe Série doit avoir une relation d’agrégation avec la classe Genre et une relation de composition avec la classe Épisode. Assurez-vous que les attributs sont correctement encapsulés et que les méthodes publiques permettent de manipuler ces objets de manière cohérente.
Enfin, écrivez un code de test pour vérifier que vos classes fonctionnent correctement ensemble. Créez des instances de Séries, ajoutez-leur des Épisodes et associez-les à des Genres. Vérifiez que les relations entre les objets sont bien gérées et que les opérations sur les objets respectent les principes d’encapsulation.
class Série:
def __init__(self, nom, genre):
self.__nom = nom
self.__genres = genre
self.__episodes = []
@property
def nom(self):
return self.__nom
@property
def genres(self):
return self.__genres
@property
def episodes(self):
return self.__episodes
def créerUnEpisode(self, nom):
episode = Episode(nom)
self.__episodes.append(episode)
class Genre:
def __init__(self, nom):
self.__nom = nom
@property
def nom(self):
return self.__nom
class Episode:
def __init__(self, nom):
self.__nom = nom
@property
def nom(self):
return self.__nom
genre = Genre("Dessin animés")
simpsons = Série("Les Simpsons", genre)
simpsons.créerUnEpisode("Noël mortel")
simpsons.créerUnEpisode("Bart le génie")
simpsons.créerUnEpisode("Un atome de bon sens")
southpark = Série("South Park", genre)
southpark.créerUnEpisode("Muscle Plus 4000")
southpark.créerUnEpisode("Volcano")
southpark.créerUnEpisode("La Mort")
series = [simpsons, southpark]
for serie in series:
print(serie.nom, ":")
for episode in serie.episodes:
print(" -", episode.nom)
print()#include <iostream>
#include <memory>
#include <vector>
#include <string>
class Genre {
public:
Genre(const std::string& nom) : _nom(nom) {}
std::string nom() const {
return _nom;
}
private:
std::string _nom;
};
class Episode {
public:
Episode(const std::string& nom) : _nom(nom) {}
std::string nom() const {
return _nom;
}
private:
std::string _nom;
};
class Serie {
public:
Serie(const std::string& nom, std::shared_ptr<Genre> genre)
: _nom(nom), _genres(genre) {}
std::string nom() const {
return _nom;
}
std::shared_ptr<Genre> genres() const {
return _genres;
}
const std::vector<std::shared_ptr<Episode>>& episodes() const {
return _episodes;
}
void creerUnEpisode(const std::string& nom) {
auto episode = std::make_shared<Episode>(nom);
_episodes.push_back(episode);
}
private:
std::string _nom;
std::shared_ptr<Genre> _genres;
std::vector<std::shared_ptr<Episode>> _episodes;
};
int main() {
auto genre = std::make_shared<Genre>("Dessin animés");
Serie simpsons("Les Simpsons", genre);
simpsons.creerUnEpisode("Noël mortel");
simpsons.creerUnEpisode("Bart le génie");
simpsons.creerUnEpisode("Un atome de bon sens");
Serie southpark("South Park", genre);
southpark.creerUnEpisode("Muscle Plus 4000");
southpark.creerUnEpisode("Volcano");
southpark.creerUnEpisode("La Mort");
std::vector<Serie> series = {simpsons, southpark};
for (const auto& serie : series) {
std::cout << serie.nom() << " :" << std::endl;
for (const auto& episode : serie.episodes()) {
std::cout << " -" << episode->nom() << std::endl;
}
std::cout << std::endl;
}
return 0;
}
using System;
using System.Collections.Generic;
public class Serie
{
public string Nom { get; }
private List<Episode> _episodes;
public IReadOnlyList<Episode> Episodes => _episodes.AsReadOnly();
public Genre Genre { get; }
public Serie(string nom, Genre genre)
{
Nom = nom;
Genre = genre;
_episodes = new List<Episode>();
}
public void CreerUnEpisode(string nom)
{
var episode = new Episode(nom);
_episodes.Add(episode);
}
}
public class Genre
{
public string Nom { get; }
public Genre(string nom)
{
Nom = nom;
}
}
public class Episode
{
public string Nom { get; }
public Episode(string nom)
{
Nom = nom;
}
}
public class Program
{
public static void Main()
{
var genre = new Genre("Dessin animés");
var simpsons = new Serie("Les Simpsons", genre);
simpsons.CreerUnEpisode("Noël mortel");
simpsons.CreerUnEpisode("Bart le génie");
simpsons.CreerUnEpisode("Un atome de bon sens");
var southpark = new Serie("South Park", genre);
southpark.CreerUnEpisode("Muscle Plus 4000");
southpark.CreerUnEpisode("Volcano");
southpark.CreerUnEpisode("La Mort");
var series = new List<Serie> { simpsons, southpark };
foreach (var serie in series)
{
Console.WriteLine(serie.Nom + ":");
foreach (var episode in serie.Episodes)
{
Console.WriteLine(" -" + episode.Nom);
}
Console.WriteLine();
}
}
}Si vous souhaitez plus d’exercices, voici d’autres schémas UML pour vous entraîner :
class Album:
def __init__(self, titre, artiste):
self.__titre = titre
self.__artiste = artiste
self.__chansons = [] # Aggrégation : les chansons existent indépendamment
@property
def titre(self):
return self.__titre
@property
def artiste(self):
return self.__artiste
@property
def chansons(self):
return self.__chansons
# Remarque : la chanson doit être créée à l'extérieur
def ajouterChanson(self, chanson):
self.__chansons.append(chanson)
class Artiste:
def __init__(self, nom):
self.__nom = nom
@property
def nom(self):
return self.__nom
class Chanson:
def __init__(self, titre):
self.__titre = titre
@property
def titre(self):
return self.__titre
artiste = Artiste("Daft Punk")
album = Album("Discovery", artiste)
chanson1 = Chanson("One More Time")
chanson2 = Chanson("Digital Love")
chanson3 = Chanson("Harder, Better, Faster, Stronger")
album.ajouterChanson(chanson1)
album.ajouterChanson(chanson2)
album.ajouterChanson(chanson3)
print("Album :", album.titre)
print("Artiste :", album.artiste.nom)
print("Chansons :")
for chanson in album.chansons:
print(" -", chanson.titre)#include <iostream>
#include <memory>
#include <vector>
#include <string>
class Artiste {
public:
Artiste(const std::string& nom) : _nom(nom) {}
std::string nom() const {
return _nom;
}
private:
std::string _nom;
};
class Chanson {
public:
Chanson(const std::string& titre) : _titre(titre) {}
std::string titre() const {
return _titre;
}
private:
std::string _titre;
};
class Album {
public:
Album(const std::string& titre, std::shared_ptr<Artiste> artiste)
: _titre(titre), _artiste(artiste) {}
std::string titre() const {
return _titre;
}
std::shared_ptr<Artiste> artiste() const {
return _artiste;
}
const std::vector<std::shared_ptr<Chanson>>& chansons() const {
return _chansons;
}
// On ajoute ici la chanson déjà créée
void ajouterChanson(std::shared_ptr<Chanson> chanson) {
_chansons.push_back(chanson);
}
private:
std::string _titre;
std::shared_ptr<Artiste> _artiste;
std::vector<std::shared_ptr<Chanson>> _chansons; // Aggrégation
};
int main() {
auto artiste = std::make_shared<Artiste>("Daft Punk");
Album album("Discovery", artiste);
// Création des chansons en dehors de l'album
auto chanson1 = std::make_shared<Chanson>("One More Time");
auto chanson2 = std::make_shared<Chanson>("Digital Love");
auto chanson3 = std::make_shared<Chanson>("Harder, Better, Faster, Stronger");
// Ajout des chansons à l'album
album.ajouterChanson(chanson1);
album.ajouterChanson(chanson2);
album.ajouterChanson(chanson3);
std::cout << "Album : " << album.titre() << std::endl;
std::cout << "Artiste : " << album.artiste()->nom() << std::endl;
std::cout << "Chansons :" << std::endl;
for (const auto& chanson : album.chansons()) {
std::cout << " - " << chanson->titre() << std::endl;
}
return 0;
}
using System;
using System.Collections.Generic;
public class Album
{
public string Titre { get; }
public Artiste Artiste { get; }
private List<Chanson> _chansons;
public IReadOnlyList<Chanson> Chansons => _chansons.AsReadOnly();
public Album(string titre, Artiste artiste)
{
Titre = titre;
Artiste = artiste;
_chansons = new List<Chanson>();
}
// La chanson est créée à l'extérieur et ajoutée ici.
public void AjouterChanson(Chanson chanson)
{
_chansons.Add(chanson);
}
}
public class Artiste
{
public string Nom { get; }
public Artiste(string nom)
{
Nom = nom;
}
}
public class Chanson
{
public string Titre { get; }
public Chanson(string titre)
{
Titre = titre;
}
}
public class Program
{
public static void Main()
{
var artiste = new Artiste("Daft Punk");
var album = new Album("Discovery", artiste);
// Création des chansons à l'extérieur de l'album
var chanson1 = new Chanson("One More Time");
var chanson2 = new Chanson("Digital Love");
var chanson3 = new Chanson("Harder, Better, Faster, Stronger");
// Ajout des chansons à l'album
album.AjouterChanson(chanson1);
album.AjouterChanson(chanson2);
album.AjouterChanson(chanson3);
Console.WriteLine("Album : " + album.Titre);
Console.WriteLine("Artiste : " + album.Artiste.Nom);
Console.WriteLine("Chansons :");
foreach (var chanson in album.Chansons)
{
Console.WriteLine(" - " + chanson.Titre);
}
}
}class Livre:
def __init__(self, titre, auteur):
self.__titre = titre
self.__auteur = auteur
self.__chapitres = []
@property
def titre(self):
return self.__titre
@property
def auteur(self):
return self.__auteur
@property
def chapitres(self):
return self.__chapitres
def ajouterChapitre(self, titre):
chapitre = Chapitre(titre)
self.__chapitres.append(chapitre)
class Auteur:
def __init__(self, nom):
self.__nom = nom
@property
def nom(self):
return self.__nom
class Chapitre:
def __init__(self, titre):
self.__titre = titre
@property
def titre(self):
return self.__titre
auteur = Auteur("Jules Verne")
livre = Livre("Voyage au centre de la Terre", auteur)
livre.ajouterChapitre("Chapitre 1 - La découverte")
livre.ajouterChapitre("Chapitre 2 - L'expédition")
livre.ajouterChapitre("Chapitre 3 - L'aventure souterraine")
print("Livre :", livre.titre)
print("Auteur :", livre.auteur.nom)
print("Chapitres :")
for chapitre in livre.chapitres:
print(" -" + chapitre.titre)#include <iostream>
#include <memory>
#include <vector>
#include <string>
class Auteur {
public:
Auteur(const std::string& nom) : _nom(nom) {}
std::string nom() const {
return _nom;
}
private:
std::string _nom;
};
class Chapitre {
public:
Chapitre(const std::string& titre) : _titre(titre) {}
std::string titre() const {
return _titre;
}
private:
std::string _titre;
};
class Livre {
public:
Livre(const std::string& titre, std::shared_ptr<Auteur> auteur)
: _titre(titre), _auteur(auteur) {}
std::string titre() const {
return _titre;
}
std::shared_ptr<Auteur> auteur() const {
return _auteur;
}
const std::vector<std::shared_ptr<Chapitre>>& chapitres() const {
return _chapitres;
}
void ajouterChapitre(const std::string& titre) {
auto chapitre = std::make_shared<Chapitre>(titre);
_chapitres.push_back(chapitre);
}
private:
std::string _titre;
std::shared_ptr<Auteur> _auteur;
std::vector<std::shared_ptr<Chapitre>> _chapitres;
};
int main() {
auto auteur = std::make_shared<Auteur>("Jules Verne");
Livre livre("Voyage au centre de la Terre", auteur);
livre.ajouterChapitre("Chapitre 1 - La découverte");
livre.ajouterChapitre("Chapitre 2 - L'expédition");
livre.ajouterChapitre("Chapitre 3 - L'aventure souterraine");
std::cout << "Livre : " << livre.titre() << std::endl;
std::cout << "Auteur : " << livre.auteur()->nom() << std::endl;
std::cout << "Chapitres :" << std::endl;
for (const auto& chapitre : livre.chapitres()) {
std::cout << " - " << chapitre->titre() << std::endl;
}
return 0;
}
using System;
using System.Collections.Generic;
public class Livre
{
public string Titre { get; }
public Auteur Auteur { get; }
private List<Chapitre> _chapitres;
public IReadOnlyList<Chapitre> Chapitres => _chapitres.AsReadOnly();
public Livre(string titre, Auteur auteur)
{
Titre = titre;
Auteur = auteur;
_chapitres = new List<Chapitre>();
}
public void AjouterChapitre(string titre)
{
var chapitre = new Chapitre(titre);
_chapitres.Add(chapitre);
}
}
public class Auteur
{
public string Nom { get; }
public Auteur(string nom)
{
Nom = nom;
}
}
public class Chapitre
{
public string Titre { get; }
public Chapitre(string titre)
{
Titre = titre;
}
}
public class Program
{
public static void Main()
{
var auteur = new Auteur("Jules Verne");
var livre = new Livre("Voyage au centre de la Terre", auteur);
livre.AjouterChapitre("Chapitre 1 - La découverte");
livre.AjouterChapitre("Chapitre 2 - L'expédition");
livre.AjouterChapitre("Chapitre 3 - L'aventure souterraine");
Console.WriteLine("Livre : " + livre.Titre);
Console.WriteLine("Auteur : " + livre.Auteur.Nom);
Console.WriteLine("Chapitres :");
foreach (var chapitre in livre.Chapitres)
{
Console.WriteLine(" - " + chapitre.Titre);
}
}
}