PHPMode Commandeimplémentation d'exemples de code simples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <?php
interface Command
{
public function execute();
}
class ConcreteCommand implements Command
{
private $receiver ;
public function construct(Receiver $r ) {
$this ->receiver = $r ;
}
public function execute() {
$this ->receiver->doAction();
}
}
class Receiver
{
public function doAction() {
echo 'Action has been taken!<br/>';
}
}
class Invoker
{
private $cmd ;
public function construct(Command $cmd ) {
$this ->cmd = $cmd ;
}
public function action() {
$this ->cmd->execute();
}
}
$r = new Receiver();
$cmd = new ConcreteCommand( $r );
$invoker = new Invoker( $cmd );
$invoker ->action();
|
Copier après la connexion
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!