Wiki

Go Search
  

Command

Le pattern commande encapsule une requête comme un objet, autorisant ainsi le paramétrage des clients par différentes requêtes, files d'attentes et récapitulatif de requêtes, et de plus, permettant la reversibilité des opérations.

Un panneau de controle permet de contrôler l'ouverture et la fermeture des volets, stores et persiennes d'une maison.
Les fabriquants des persiennes, stores ou volets de la maison ont fournis un ensemble de classe "api" d'interfaces différentes, afin de controler par programme l'ouverture et la fermeture de celles ci.
Des commandes respectants l'interface ICommand compatible avec le panneau de controle sont créées.
Un programmateur programme le panneau de controle pour associer un élément a un switch du panneau de contrôle.

Les classes fournis par les constructeurs de persiennes, stores et volets:

Class Store
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public class StoreSalleAManger
  8.     {
  9.         public void Abaisser()
  10.         {
  11.             Console.WriteLine("Store Abaissé");
  12.         }
  13.         public void Relever()
  14.         {
  15.             Console.WriteLine("Store Relevé");
  16.         }
  17.     }
  18. }

Class Persienne
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public class PersienneBaieVitree
  8.     {
  9.         public void Descendre()
  10.         {
  11.             Console.WriteLine("Persienne Descendue");
  12.         }
  13.         public void Monter()
  14.         {
  15.             Console.WriteLine("Persienne Montée");
  16.         }
  17.     }
  18. }

Class Volet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public class VoletCuisine
  8.     {
  9.         public void Ouvrir()
  10.         {
  11.             Console.WriteLine("Volet Ouvert");
  12.         }
  13.         public void Fermer()
  14.         {
  15.             Console.WriteLine("Volet Fermé");
  16.         }
  17.     }
  18. }

Les commandes qui encapsulent l'appel au API des fabricants. Avec l'interface ICommand:

Interface ICommand
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public interface ICommand
  8.     {
  9.         void Open();
  10.         void Close();
  11.     }
  12. }

Class CommandStore
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public class CommandeStore : ICommand
  8.     {
  9.         StoreSalleAManger store;
  10.         public CommandeStore(StoreSalleAManger store)
  11.         {
  12.             this.store = store;
  13.         }
  14.         #region ICommand Members
  15.         public void Open()
  16.         {
  17.             this.store.Relever();
  18.         }
  19.         public void Close()
  20.         {
  21.             this.store.Abaisser();
  22.         }
  23.         #endregion
  24.     }
  25. }

Class CommandPersienne
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public class CommandPersienne : ICommand
  8.     {
  9.         PersienneBaieVitree persienne;
  10.         public CommandPersienne(PersienneBaieVitree persienne)
  11.         {
  12.             this.persienne = persienne;
  13.         }
  14.         #region ICommand Members
  15.         public void Open()
  16.         {
  17.             this.persienne.Monter();
  18.         }
  19.         public void Close()
  20.         {
  21.             this.persienne.Descendre();
  22.         }
  23.         #endregion
  24.     }
  25. }

Class CommandVolet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public class CommandVolet : ICommand
  8.     {
  9.         VoletCuisine volet;
  10.         public CommandVolet(VoletCuisine volet)
  11.         {
  12.             this.volet = volet;
  13.         }
  14.         #region ICommand Members
  15.         public void Open()
  16.         {
  17.             this.volet.Ouvrir();
  18.         }
  19.         public void Close()
  20.         {
  21.             this.volet.Fermer();
  22.         }
  23.         #endregion
  24.     }
  25. }

Le panneau de contrôle utilisant les ICommand avec les swicths:

Class PanneauControle
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public class PanneauControl
  8.     {
  9.         ICommand switch1;
  10.         ICommand switch2;
  11.         ICommand switch3;
  12.         public void SetCommandSwitch1(ICommand Command)
  13.         {
  14.             switch1=Command ;
  15.         }
  16.         public void SetCommandSwitch2(ICommand Command)
  17.         {
  18.             switch2 = Command;
  19.         }
  20.         public void SetCommandSwitch3(ICommand Command)
  21.         {
  22.             switch3 = Command;
  23.         }
  24.         public void Switch1Open()
  25.         {
  26.             switch1.Open();
  27.         }
  28.         public void Switch1Close()
  29.         {
  30.             switch1.Close();
  31.         }
  32.         public void Switch2Open()
  33.         {
  34.             switch2.Open();
  35.         }
  36.         public void Switch2Close()
  37.         {
  38.             switch2.Close();
  39.         }
  40.         public void Switch3Open()
  41.         {
  42.             switch3.Open();
  43.         }
  44.         public void Switch3Close()
  45.         {
  46.             switch3.Close();
  47.         }
  48.         
  49.     }
  50. }

Le Programmateur du panneau de contrôle:

Class Programmateur
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     public class Programmateur
  8.     {
  9.         public static PanneauControl ProgrammerPanneau()
  10.         {
  11.             PersienneBaieVitree persienne = new PersienneBaieVitree();
  12.             StoreSalleAManger store = new StoreSalleAManger();
  13.             VoletCuisine volet = new VoletCuisine();
  14.             CommandeStore cmdStore = new CommandeStore(store);
  15.             CommandPersienne cmdPersienne = new CommandPersienne(persienne);
  16.             CommandVolet cmdVolet = new CommandVolet(volet);
  17.             PanneauControl pnl = new PanneauControl();
  18.             pnl.SetCommandSwitch1(cmdVolet);
  19.             pnl.SetCommandSwitch2(cmdPersienne);
  20.             pnl.SetCommandSwitch3(cmdStore);
  21.             return pnl;
  22.         }
  23.     }
  24. }

Le Client qui programme le panneau de contrôle et l'appel:

Class Program
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CommandPattern
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             PanneauControl pnl= Programmateur.ProgrammerPanneau();
  12.             pnl.Switch1Open();
  13.             pnl.Switch2Close();
  14.             pnl.Switch3Open();
  15.             pnl.Switch3Close();
  16.             Console.ReadLine();
  17.         }
  18.     }
  19. }

Le résultat montre que les switchs sont associés aux commandes. Ils peuvent être intervertis simplement dans les swicths

Last modified at 11/12/2009 3:32 PM  by ASPHOST150\xvanneste