Wzorzec komendy

<?php
//Programowanie obiektowe w języku PHP5 / Moduł 5. SOLID / Cz. 2
require_once 'lib/logic/Aplication.php';
require_once 'lib/logic/AplicationModel.php';

interface Command {
  public function execute($parameters);
  public function printHelp();
}

class Action {
  public function execute() {}
}

class HellowAction extends Action {
  public function __construct($app) {
    $this->app = $app;
  }

  function execute() {
    $this->app->hellow();
  }
}

class LogoutAction extends Action {
  public function __construct($app) {
    $this->app = $app;
  }

  function execute() {
    $this->app->logout();
  }
}

//wzorzec komendy

class RequestProcess {
  public function process() {
    session_start();
    $app = new Aplication;
    $aplicationmodel = new AplicationModel;

    $actions['hellow'] = new HellowAction($app);
    $actions['logout'] = new LogoutAction($app);

    include 'template/header.php';
    include 'template/menu.php';

    $actionName = $_GET['action'];
    $action = $actions[$actionName];

    $action->execute();

    include 'template/footer.php';

  }
}
?>

To samo bez zastosowania wzoraca:

<?php

require_once 'lib/logic/Aplication.php';
require_once 'lib/logic/AplicationModel.php';

class RequestProcess {

  function __construct() {

  }

  public function process() {
    session_start();
    $app = new Aplication;
    $aplicationmodel = new AplicationModel;

    include 'template/header.php';
    include 'template/menu.php';

    if (isset($_GET['action']) && $_GET['action'] == 'hellow') {
      $app->Hellow();
    } elseif (isset($_GET['action']) && $_GET['action'] == 'logout') {
      $app->logout();
    }

    //z

    include 'template/footer.php';

  }
}
?>
Komentarze wyłączone