
1、把请求封装成一个对象,允许使用不同的请求来参数化客户。对请求进行排队或记录请求日志,并支持可撤销动作。
2、组成命令抽象类、具体命令类等。
命令抽象类,用来声明执行操作的接口。
具体命令类,将一个接收者对象绑定于一个动作,调用接收者相应的操作。
命令发送者,要求该命令执行这个请求。
命令接收者,知道如何实施与执行一个请求相关的操作,任何类都可能作为一个接收者。
客户端代码,创建一个具体命令对象并设定它的接收者。
实例
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 | <?php
abstract class Command{
abstract function Excute();
}
class ConcreteCommand extends Command{
private $Receiver ;
function __construct(Receiver $Receiver )
{
$this ->Receiver = $Receiver ;
}
function Excute()
{
$this ->Receiver->DoSomething();
}
}
class Receiver{
function DoSomething()
{
echo "Receiver do something." ;
}
}
class Invoker{
private $Command ;
function __construct(Command $Command )
{
$this ->Command = $Command ;
}
function Action()
{
$this ->Command->Excute();
}
}
$Receiver = new Receiver();
$Command = new ConcreteCommand( $Receiver );
$Command ->Excute();
$Invoker = new Invoker( $Command );
$Invoker ->Action();
?>
|
以上就是php命令模式的理解,希望对大家有所帮助。更多php学习指路:php教程
推荐操作系统:windows7系统、PHP5.6、DELL G3电脑