0;
}
}
/**
* Class ConcreteCommandTwo 具体命令2
*/
class ConcreteCommandTwo implements Command
{
/**
* 实现验证方法
*
* @param $value
*
* @return bool
*/
public function isValid($value)
{
// 能被2整除的数字
return $value % 2 == 0;
}
}
/**
* Class Invoker 调用者
*/
class Invoker
{
protected $_rule;
/**
* 构造方法
* 接收具体命令对象
* Invoker constructor.
*
* @param Command $rule
*/
public function __construct (Command $rule)
{
$this->_rule = $rule;
}
public function process(array $numbers)
{
foreach ($numbers as $n) {
if ($this->_rule->IsValid($n)) {
echo $n, "\n";
}
}
}
}
/**
* Class Client 客户端
*/
class Client {
/**
* 测试
*/
public static function test()
{
$invoker = new Invoker(new ConcreteCommand());
$invoker->process(array(-1,-4,-8,1, 10, 15, 20, 36, 48, 59,111));
echo '
';
$invoker = new Invoker(new ConcreteCommandTwo());
$invoker->process(array(-1,-4,-8,1, 10, 15, 20, 36, 48, 59,111));
}
}
// 执行测试
Client::test();
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)