« Patrons de conception/Commande » : différence entre les versions

Contenu supprimé Contenu ajouté
DannyS712 (discussion | contributions)
m <source> -> <syntaxhighlight> (phab:T237267)
Copie de WP : C# & PHP
Ligne 29 :
 
== Exemple ==
 
=== [[Programmation C sharp|C#]] ===
<syntaxhighlight lang="csharp">
using System;
using System.Collections.Generic;
 
namespace CommandPattern
{
public interface ICommand
{
void Execute();
}
 
/* The Invoker class */
public class Switch
{
private List<ICommand> _commands = new List<ICommand>();
 
public void StoreAndExecute(ICommand command)
{
_commands.Add(command);
command.Execute();
}
}
 
/* The Receiver class */
public class Light
{
public void TurnOn()
{
Console.WriteLine("The light is on");
}
 
public void TurnOff()
{
Console.WriteLine("The light is off");
}
}
 
/* The Command for turning on the light - ConcreteCommand #1 */
public class FlipUpCommand : ICommand
{
private Light _light;
 
public FlipUpCommand(Light light)
{
_light = light;
}
 
public void Execute()
{
_light.TurnOn();
}
}
 
/* The Command for turning off the light - ConcreteCommand #2 */
public class FlipDownCommand : ICommand
{
private Light _light;
 
public FlipDownCommand(Light light)
{
_light = light;
}
 
public void Execute()
{
_light.TurnOff();
}
}
 
/* The test class or client */
internal class Program
{
public static void Main(string[] args)
{
Light lamp = new Light();
ICommand switchUp = new FlipUpCommand(lamp);
ICommand switchDown = new FlipDownCommand(lamp);
 
Switch s = new Switch();
string arg = args.Length > 0 ? args[0].ToUpper() : null;
switch(arg)
{
case "ON":
s.StoreAndExecute(switchUp);
break;
case "OFF":
s.StoreAndExecute(switchDown);
break;
default:
Console.WriteLine("Argument \"ON\" or \"OFF\" is required.");
break;
}
}
}
}</syntaxhighlight>
 
=== [[Programmation Java|Java]] ===
Ligne 186 ⟶ 283 :
 
Cela convertit les appels aux différentes méthodes dans l'objet courant, en appels à une méthode ''handleCommand'' dans différents objets. Cet exemple utilise Perl pour adapter un patron de conception à base d'objets Commandes, dans une interface qui en est dépourvue.
 
=== [[Programmation PHP|PHP]] ===
<syntaxhighlight lang="PHP">
abstract class Command
{
abstract function execute();
}
 
class Executor
{
private array $history = [];
 
public function saveAndExecute(Command $cmd)
{
$this->history[] = $cmd; // optional
$cmd->execute();
}
}
 
class Light
{
public function turnOn()
{
writeLine('Light is on.');
}
 
public function turnOff()
{
writeLine('Light is off.');
}
}
 
class CommandOn extends Command
{
public function __construct(private readonly Light $light)
{
}
 
public function execute()
{
$this->light->turnOn();
}
}
 
class CommandOff extends Command
{
public function __construct(private readonly Light $light)
{
}
 
public function execute()
{
$this->light->turnOff();
}
}
 
function Client($commandString)
{
$lamp = new Light();
$cmdOn = new CommandOn($lamp);
$cmdOff = new CommandOff($lamp);
 
$executor = new Executor();
 
switch ($commandString) {
case 'ON':
$executor->saveAndExecute($cmdOn);
break;
case 'OFF':
$executor->saveAndExecute($cmdOff);
break;
default:
writeLine('Please ask for "ON" or "OFF" only.');
}
}
 
function writeLine($text) {
print $text.'<br/>';
}
 
Client('ON');
Client('OFF');
</syntaxhighlight>
Cela affiche :
Light is on.
Light is off.