PHP面向对象中的命令模式的构成以及作用
创始人
2024-12-08 17:02:10
0次

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

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

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

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

4、命令工厂类(我自己取的名字,哈哈):生成命令类的实例,这个类第一次看的时候我觉得有点屌,当然看了几遍了还是觉得很屌 :);

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

直接看代码:

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

class LoginCommand extends Command{       //处理用户登陆信息的命令类
  function execute (CommandCotext $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
  }
}

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";
    $file = self::$dir.DIRECTORY_SEPARATOR."{$class}.php"; //DIRECTORY_SEPARATOR代表'/',这是一个命令类文件的路径
    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(!$comd->execute($this->context)){                      
      //处理失败
    } else {
      //成功
      // 分发视图
    }
  }
}

// 客户端
$controller = new Controller();
//伪造用户请求,真实的场景中这些参数应该是通过post或get的方式获取的,貌似又废话了:)
$context = $controller->getContext();
$context->addParam('action','login');
$context->addParam('username','bob');
$context->addParam('pass','tiddles');
$controller->process();

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...