{"id":367,"date":"2025-03-22T11:45:02","date_gmt":"2025-03-22T10:45:02","guid":{"rendered":"https:\/\/wmg-studio.com\/?p=367"},"modified":"2025-03-22T11:51:45","modified_gmt":"2025-03-22T10:51:45","slug":"principes-solid","status":"publish","type":"post","link":"https:\/\/wmg-studio.com\/index.php\/2025\/03\/22\/principes-solid\/","title":{"rendered":"principes SOLID"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"fr\">\n<style>\n\/* style.css *\/\n\n.body {\n  font-family: 'Segoe UI', sans-serif;\n  margin: 0;\n  padding: 0;\n  background-color: #f4f7fa;\n  color: #333;\n}\n\n.section {\n  padding: 2rem;\n\n  margin-bottom: 10.5px;\n  max-width: 1000px;\n  border-radius: 10px;\n  background-color: #ffffff;\n  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);\n}\n\n.heading--main {\n  font-size: 2rem;\n  color: #2c3e50;\n  border-bottom: 2px solid #3498db;\n  padding-bottom: 0.5rem;\n  margin-bottom: 1.5rem;\n}\n\n.heading--principle,\n.heading--correction {\n  font-size: 1.5rem;\n  color: #34495e;\n  margin-top: 2rem;\n}\n\n.heading--example {\n  font-size: 1.2rem;\n  color: #7f8c8d;\n  margin-top: 1rem;\n}\n\n.text--intro,\n.text--principle,\n.text--exercise,\n.text--conclusion {\n  font-size: 1.1rem;\n  line-height: 1.6;\n  margin-bottom: 1.5rem;\n}\n\n.table--solid {\n  width: 100%;\n  border-collapse: collapse;\n  margin-top: 1rem;\n  background-color: #ecf0f1;\n  border: 1px solid #bdc3c7;\n}\n\n.table--solid th {\n  background-color: #3498db;\n  color: white;\n  padding: 0.75rem;\n}\n\n.table--solid td {\n  padding: 0.75rem;\n  border-top: 1px solid #bdc3c7;\n}\n\n.code {\n  background-color: #2c3e50;\n  color: #ecf0f1;\n  padding: 1rem;\n  border-radius: 5px;\n  font-family: 'Courier New', monospace;\n  overflow-x: auto;\n  margin-top: 1rem;\n  margin-bottom: 1.5rem;\n}\n\n.example--bad {\n  background-color: #fdecea;\n  border-left: 5px solid #e74c3c;\n  padding: 1rem;\n  margin-bottom: 1rem;\n  border-radius: 5px;\n}\n\n.example--good {\n  background-color: #e8f8f5;\n  border-left: 5px solid #27ae60;\n  padding: 1rem;\n  margin-bottom: 2rem;\n  border-radius: 5px;\n}\n\n.correction__explanation {\n  font-style: italic;\n  color: #7f8c8d;\n  margin-bottom: 0.5rem;\n}\n\n<\/style>\n<head>\n  <meta charset=\"UTF-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n  <title>Cours complet &#8211; Principes SOLID en .NET<\/title>\n  <link rel=\"stylesheet\" href=\"style.css\">\n<\/head>\n<body class=\"body\">\n  <section class=\"section section--intro\">\n    <h1 class=\"heading heading--main\">1. Introduction aux principes SOLID<\/h1>\n    <p class=\"text text--intro\">\n      Les principes SOLID sont les fondations du d\u00e9veloppement orient\u00e9 objet moderne. Ils visent \u00e0 am\u00e9liorer la qualit\u00e9 du code, \u00e0 le rendre plus lisible, modulaire, et facilement testable. Voici un r\u00e9sum\u00e9 des 5 principes :\n    <\/p>\n    <table class=\"table table--solid\">\n      <thead>\n        <tr><th>Lettre<\/th><th>Nom<\/th><th>Description<\/th><\/tr>\n      <\/thead>\n      <tbody>\n        <tr><td>S<\/td><td>Single Responsibility Principle<\/td><td>Une classe doit avoir une seule responsabilit\u00e9<\/td><\/tr>\n        <tr><td>O<\/td><td>Open\/Closed Principle<\/td><td>Ouvert \u00e0 l&rsquo;extension, ferm\u00e9 \u00e0 la modification<\/td><\/tr>\n        <tr><td>L<\/td><td>Liskov Substitution Principle<\/td><td>Une sous-classe doit pouvoir remplacer sa classe m\u00e8re<\/td><\/tr>\n        <tr><td>I<\/td><td>Interface Segregation Principle<\/td><td>Pr\u00e9f\u00e9rer plusieurs interfaces sp\u00e9cifiques \u00e0 une interface g\u00e9n\u00e9rale<\/td><\/tr>\n        <tr><td>D<\/td><td>Dependency Inversion Principle<\/td><td>D\u00e9pendre d\u2019abstractions, pas de classes concr\u00e8tes<\/td><\/tr>\n      <\/tbody>\n    <\/table>\n  <\/section>\n\n  <section class=\"section section--principes\">\n    <h1 class=\"heading heading--main\">2. Les principes SOLID expliqu\u00e9s avec coh\u00e9rence<\/h1>\n\n    <h2 class=\"heading heading--principle\">2.1 &#8211; SRP : Une seule responsabilit\u00e9<\/h2>\n    <p class=\"text text--principle\">Un service de commande ne devrait pas g\u00e9rer l\u2019envoi d\u2019email, ni la g\u00e9n\u00e9ration de factures. Chaque classe a une seule t\u00e2che.<\/p>\n    <div class=\"example example--bad\">\n      <h3 class=\"heading heading--example\">Mauvais exemple<\/h3>\n      <pre class=\"code\">\npublic class CommandeService {\n    public void Cr\u00e9erCommande() {}\n    public void EnvoyerConfirmationEmail() {}\n    public void G\u00e9n\u00e9rerFacture() {}\n}<\/pre>\n    <\/div>\n    <div class=\"example example--good\">\n      <h3 class=\"heading heading--example\">Bon exemple<\/h3>\n      <pre class=\"code\">\npublic class CommandeService {\n    public void Cr\u00e9erCommande() {}\n}\n\npublic class EmailService {\n    public void EnvoyerConfirmationEmail() {}\n}\n\npublic class FactureService {\n    public void G\u00e9n\u00e9rerFacture() {}\n}<\/pre>\n    <\/div>\n\n    <h2 class=\"heading heading--principle\">2.2 &#8211; OCP : Ouvert \u00e0 l&rsquo;extension, ferm\u00e9 \u00e0 la modification<\/h2>\n    <p class=\"text text--principle\">Si on veut ajouter un nouveau type de livraison, on ne devrait pas modifier le service existant.<\/p>\n    <div class=\"example example--bad\">\n      <pre class=\"code\">\npublic class CalculateurLivraison {\n    public double CalculerFrais(string type) {\n        if (type == \"standard\") return 5;\n        if (type == \"express\") return 10;\n        return 0;\n    }\n}<\/pre>\n    <\/div>\n    <div class=\"example example--good\">\n      <pre class=\"code\">\npublic interface ICalculateurFrais {\n    double Calculer();\n}\n\npublic class LivraisonStandard : ICalculateurFrais {\n    public double Calculer() => 5;\n}\n\npublic class LivraisonExpress : ICalculateurFrais {\n    public double Calculer() => 10;\n}<\/pre>\n    <\/div>\n\n    <h2 class=\"heading heading--principle\">2.3 &#8211; LSP : Substitution coh\u00e9rente<\/h2>\n    <p class=\"text text--principle\">Une classe enfant ne doit pas casser les attentes de la classe parent.<\/p>\n    <div class=\"example example--bad\">\n      <pre class=\"code\">\npublic class MoyennePaiement {\n    public virtual void EffectuerPaiement() {}\n}\n\npublic class PaiementTest : MoyennePaiement {\n    public override void EffectuerPaiement() {\n        throw new NotSupportedException();\n    }\n}<\/pre>\n    <\/div>\n    <div class=\"example example--good\">\n      <pre class=\"code\">\npublic interface IPaiement {\n    void EffectuerPaiement();\n}\n\npublic class PaiementCarte : IPaiement {\n    public void EffectuerPaiement() {}\n}\n\npublic class PaiementVirement : IPaiement {\n    public void EffectuerPaiement() {}\n}<\/pre>\n    <\/div>\n\n    <h2 class=\"heading heading--principle\">2.4 &#8211; ISP : Interfaces sp\u00e9cifiques<\/h2>\n    <p class=\"text text--principle\">Les interfaces doivent \u00eatre courtes et cibl\u00e9es.<\/p>\n    <div class=\"example example--bad\">\n      <pre class=\"code\">\npublic interface IServiceClient {\n    void Commander();\n    void Payer();\n    void NotifierClient();\n}<\/pre>\n    <\/div>\n    <div class=\"example example--good\">\n      <pre class=\"code\">\npublic interface ICommandable {\n    void Commander();\n}\n\npublic interface IPayable {\n    void Payer();\n}\n\npublic interface INotifiable {\n    void NotifierClient();\n}<\/pre>\n    <\/div>\n\n    <h2 class=\"heading heading--principle\">2.5 &#8211; DIP : D\u00e9pendre d\u2019abstractions<\/h2>\n    <p class=\"text text--principle\">Une classe ne doit pas cr\u00e9er ses d\u00e9pendances mais les recevoir de l\u2019ext\u00e9rieur.<\/p>\n    <div class=\"example example--bad\">\n      <pre class=\"code\">\npublic class NotificationService {\n    private EmailService email = new EmailService();\n}<\/pre>\n    <\/div>\n    <div class=\"example example--good\">\n      <pre class=\"code\">\npublic class NotificationService {\n    private readonly IEmailService _emailService;\n\n    public NotificationService(IEmailService emailService) {\n        _emailService = emailService;\n    }\n}<\/pre>\n    <\/div>\n  <\/section>\n\n  <section class=\"section section--exercice\">\n    <h1 class=\"heading heading--main\">3. \u00c9nonc\u00e9 de l\u2019exercice<\/h1>\n    <p class=\"text text--exercise\">\n      Vous avez un service qui g\u00e8re la commande, l\u2019envoi d\u2019email, le stockage et la g\u00e9n\u00e9ration de facture. Ce service viole plusieurs principes SOLID. Refactorez-le en respectant chaque principe un par un.\n    <\/p>\n    <pre class=\"code\">\npublic class CommandeService {\n    public void Cr\u00e9erCommande() {}\n    public void EnvoyerEmail() {}\n    public void SauvegarderCommande() {}\n    public void G\u00e9n\u00e9rerFacture() {}\n}<\/pre>\n  <\/section>\n\n  <section class=\"section section--correction\">\n    <h1 class=\"heading heading--main\">4. Correction \u00e9tape par \u00e9tape<\/h1>\n\n    <div class=\"correction__step\">\n      <h2 class=\"heading heading--correction\">\u00c9tape 1 &#8211; SRP<\/h2>\n      <p class=\"correction__explanation\">On s\u00e9pare chaque action dans sa propre classe.<\/p>\n      <pre class=\"code\">\npublic class CommandeService { public void Cr\u00e9erCommande() {} }\npublic class EmailService { public void EnvoyerEmail() {} }\npublic class FactureService { public void G\u00e9n\u00e9rerFacture() {} }\npublic class CommandeRepository { public void SauvegarderCommande() {} }<\/pre>\n    <\/div>\n\n    <div class=\"correction__step\">\n      <h2 class=\"heading heading--correction\">\u00c9tape 2 &#8211; OCP<\/h2>\n      <p class=\"correction__explanation\">On permet l\u2019ajout de nouvelles strat\u00e9gies de facturation sans modifier le code existant.<\/p>\n      <pre class=\"code\">\npublic interface IStrategieFacture {\n    void G\u00e9n\u00e9rer();\n}\n\npublic class FacturePDF : IStrategieFacture {\n    public void G\u00e9n\u00e9rer() {}\n}<\/pre>\n    <\/div>\n\n    <div class=\"correction__step\">\n      <h2 class=\"heading heading--correction\">\u00c9tape 3 &#8211; LSP<\/h2>\n      <p class=\"correction__explanation\">Les classes de notifications respectent l\u2019interface et peuvent \u00eatre substitu\u00e9es.<\/p>\n      <pre class=\"code\">\npublic interface INotification {\n    void Envoyer();\n}\n\npublic class EmailNotification : INotification {\n    public void Envoyer() {}\n}<\/pre>\n    <\/div>\n\n    <div class=\"correction__step\">\n      <h2 class=\"heading heading--correction\">\u00c9tape 4 &#8211; ISP<\/h2>\n      <p class=\"correction__explanation\">On d\u00e9coupe les interfaces pour que chaque classe n\u2019impl\u00e9mente que ce dont elle a besoin.<\/p>\n      <pre class=\"code\">\npublic interface ICommandeService { void Cr\u00e9erCommande(); }\npublic interface IFacturation { void G\u00e9n\u00e9rerFacture(); }<\/pre>\n    <\/div>\n\n    <div class=\"correction__step\">\n      <h2 class=\"heading heading--correction\">\u00c9tape 5 &#8211; DIP<\/h2>\n      <p class=\"correction__explanation\">On injecte les d\u00e9pendances via le constructeur plut\u00f4t que de les instancier directement.<\/p>\n      <pre class=\"code\">\npublic class GestionCommande {\n    private readonly ICommandeService _commande;\n    public GestionCommande(ICommandeService commande) {\n        _commande = commande;\n    }\n}<\/pre>\n    <\/div>\n  <\/section>\n\n  <section class=\"section section--conclusion\">\n    <h1 class=\"heading heading--main\">5. Conclusion<\/h1>\n    <p class=\"text text--conclusion\">\n      Les principes SOLID vous aident \u00e0 concevoir des architectures logicielles \u00e9volutives et robustes. En appliquant ces principes au quotidien, vous am\u00e9liorez la qualit\u00e9 de vos projets, la collaboration en \u00e9quipe et la maintenabilit\u00e9 de votre code.\n    <\/p>\n  <\/section>\n<\/body>\n<\/html>\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cours complet &#8211; Principes SOLID en .NET 1. Introduction aux principes SOLID Les principes SOLID sont les fondations du d\u00e9veloppement orient\u00e9 objet moderne. Ils visent \u00e0 am\u00e9liorer la qualit\u00e9 du code, \u00e0 le rendre plus lisible, modulaire, et facilement testable. Voici un r\u00e9sum\u00e9 des 5 principes : Lettre Nom Description S Single Responsibility Principle Une classe doit avoir une seule responsabilit\u00e9 O Open\/Closed Principle Ouvert \u00e0 l&rsquo;extension, ferm\u00e9 \u00e0 la modification L Liskov Substitution Principle Une sous-classe doit pouvoir remplacer sa classe m\u00e8re I Interface Segregation Principle Pr\u00e9f\u00e9rer plusieurs interfaces sp\u00e9cifiques \u00e0 une interface g\u00e9n\u00e9rale D Dependency Inversion Principle D\u00e9pendre<\/p>\n","protected":false},"author":1,"featured_media":249,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-367","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developpement-avance"],"_links":{"self":[{"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":23,"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":390,"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/posts\/367\/revisions\/390"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/media\/249"}],"wp:attachment":[{"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wmg-studio.com\/index.php\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}