首页 > PHP教程 > php开发知识文章

PHP设计模式之命令模式详解

下面小编就为大家带来一篇PHP设计模式之命令模式详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

这个模式由命令类、用户请求数据类、业务逻辑类、命令类工厂类及调用类构成,各个类的作用概括如下:

1、命令类:调用用户请求数据类和业务逻辑类;

2、用户请求数据类:获取用户请求数据及保存后台处理后返回的结果;

3、业务逻辑类:如以下的示例中验证用户登陆信息是否正确的功能等;

4、命令工厂类:生成命令类的实例;

5、调用类:调用命令类,生成视图;

php代码如下

命令类:

//命令类
abstract class Command
{
abstract function execute(CommandContext $context);
}

处理用户登陆信息的命令类:LoginCommand.php文件

//处理用户登陆信息的命令类
class LoginCommand extends Command
{
function execute(CommandContext $context)
{
//CommandCotext 是一个处理用户请求数据和后台回馈数据的类
$manager = Registry::getAccessManager();//处理用户登陆信息的业务逻辑类
$user = $context->get('username');
$pass = $context->get('pass');
$user_obj = $manager->login($user, $pass);
if (is_null($user_obj)) {
$context->setError($manager->getError);
return false;
}
$context->addParam('user', $user_obj);
return true;//用户登陆成功返回true
}
}

发送邮件的命令类:FeedbackCommand.php文件

class FeedbackCommand extends Command
{
function execute(CommandContext $context)
{
$msgSystem = Registry::getMessageSystem();
$email = $context->get('email');
$msg = $context->get('msg');
$topic = $context->get('topci');
$result = $msgSystem->send($email, $msg, $topic);
if (!$result) {
$context->setError($msgSystem->getError());
return false;
}
return true;
}
}

调用文件

//用户请求数据类
class CommandContext
{
private $params = array();
private $error = '';

function __construct()
{
$this->params = $_REQUEST;
}

function addParam($key, $val)
{
$this->params[$key] = $val;
}

function get($key)
{
return $this->params[$key];
}

function setError($error)
{
$this->error = $error;
}

function getError()
{
return $this->error;
}
}


//命令类工厂,这个类根据用户请求数据中的action来生成命令类
class CommandNotFoundException extends Exception
{
}

class CommandFactory
{
private static $dir = 'commands';

static function getCommand($action = 'Default')
{
if (preg_match('/\w', $action)) {
throw new Exception("illegal characters in action");
}
$class = UCFirst(strtolower($action)) . "Command";
//DIRECTORY_SEPARATOR代表'/',这是一个命令类文件的路径
$file = self::$dir . DIRECTORY_SEPARATOR . "{$class}.php";
if (!file_exists($file)) {
throw new CommandNotFoundException("could not find '$file'");
}
require_once($file);
if (!class_exists($class)) {
throw new CommandNotFoundException("no '$class' class located");
}
$cmd = new $class();
return $cmd;
}
}

//调用者类,相当于一个司令部它统筹所有的资源
class Controller
{
private $context;

function __construct()
{
$this->context = new CommandContext(); //用户请求数据
}

function getContext()
{
return $this->context;
}

function process()
{
$cmd = CommandFactory::getCommand($this->context->get('action'));
//通过命令工厂类来获取命令类
if (!$cmd->execute($this->context)) {
//处理失败
} else {
//成功
//跳转视图
}
}
}

// 客户端
$controller = new Controller();
//伪造用户请求,真实的场景中这些参数应该是通过postget的方式获取的
$context = $controller->getContext();
$context->addParam('action', 'login');
$context->addParam('username', 'aa');
$context->addParam('pass', '123456');
$controller->process();

以上这篇PHP设计模式之命令模式详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持本站。

PHP基本语法

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。