Skip to content

Instantly share code, notes, and snippets.

@Bolinha1
Last active August 29, 2015 14:04
Show Gist options
  • Save Bolinha1/1293e3d2a5e5c7c8380d to your computer and use it in GitHub Desktop.
Save Bolinha1/1293e3d2a5e5c7c8380d to your computer and use it in GitHub Desktop.
Command Pattern
<?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