<?php
// 抽象命令类
abstract class Command
{
protected $receiver;
public function __construct(Receiver $receiver)
{
$this->receiver=$receiver;
}
abstract public function execute();
}
// 具体命令类A
class ConcreteCommandA extends Command
{
public function execute()
{
$this->receiver->actionA();
}
}
// 具体命令类B
class ConcreteCommandB extends Command
{
public function execute()
{
$this->receiver->actionB();
}
}
// 接收者类
class Receiver
{
public function actionA()
{
echo “Receiver executes actionA.\n”;
}
public function actionB()
{
echo “Receiver executes actionB.\n”;
}
}
// 客户端代码
$receiver=new Receiver();
$commandA=new ConcreteCommandA($receiver);
$commandB=new ConcreteCommandB($receiver);
$commandA->execute();
$commandB->execute();
// 抽象命令类
abstract class Command
{
protected $receiver;
public function __construct(Receiver $receiver)
{
$this->receiver=$receiver;
}
abstract public function execute();
}
// 具体命令类A
class ConcreteCommandA extends Command
{
public function execute()
{
$this->receiver->actionA();
}
}
// 具体命令类B
class ConcreteCommandB extends Command
{
public function execute()
{
$this->receiver->actionB();
}
}
// 接收者类
class Receiver
{
public function actionA()
{
echo “Receiver executes actionA.\n”;
}
public function actionB()
{
echo “Receiver executes actionB.\n”;
}
}
// 客户端代码
$receiver=new Receiver();
$commandA=new ConcreteCommandA($receiver);
$commandB=new ConcreteCommandB($receiver);
$commandA->execute();
$commandB->execute();
© 版权声明
文章版权归作者所有,未经允许请勿转载。