Last active
August 29, 2015 14:04
-
-
Save Bolinha1/1293e3d2a5e5c7c8380d to your computer and use it in GitHub Desktop.
Command Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// the command interface | |
interface Command | |
{ | |
public function execute(); | |
} | |
//the invoker | |
class Controller | |
{ | |
public function execute(Command $command) | |
{ | |
return $command->execute(); | |
} | |
} | |
//the receiver | |
class Action | |
{ | |
public function insert() | |
{ | |
return print(" \n insert ok"); | |
} | |
public function find() | |
{ | |
return print(" \n find ok"); | |
} | |
} | |
//concrete command 1 | |
class InsertCommand implements Command | |
{ | |
private $action; | |
public function __construct(Action $action) | |
{ | |
$this->action = $action; | |
} | |
public function execute() | |
{ | |
$this->action->insert(); | |
} | |
} | |
//concrete command 2 | |
class FindCommand implements Command | |
{ | |
private $action; | |
public function __construct(Action $action) | |
{ | |
$this->action = $action; | |
} | |
public function execute() | |
{ | |
$this->action->find(); | |
} | |
} | |
//client | |
$action = new Action(); | |
$insert = new InsertCommand($action); | |
$find = new FindCommand($action); | |
$controller = new Controller(); | |
$controller->execute($insert); | |
$controller->execute($find); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment