Skip to content

Commit 43d87aa

Browse files
committed
Added Command Design Pattern
1 parent d3b3785 commit 43d87aa

File tree

9 files changed

+216
-0
lines changed

9 files changed

+216
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Behavioral\Command;
4+
5+
/**
6+
* Interface CommandInterface
7+
* @package PhpDesignPatterns\Behavioral\Command
8+
*/
9+
interface CommandInterface
10+
{
11+
/**
12+
* @return mixed
13+
*/
14+
public function execute();
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Behavioral\Command;
4+
5+
/**
6+
* Class CommandInvoker
7+
* @package PhpDesignPatterns\Behavioral\Command
8+
*/
9+
class CommandInvoker
10+
{
11+
/**
12+
* Instance of CommandInterface.
13+
*
14+
* @var CommandInterface
15+
*/
16+
private $command;
17+
18+
/**
19+
* Set command dependency.
20+
*
21+
* @param CommandInterface $command
22+
* @return CommandInvoker
23+
*/
24+
public function setCommand(CommandInterface $command)
25+
{
26+
$this->command = $command;
27+
return $this;
28+
}
29+
30+
/**
31+
* Run the command.
32+
*
33+
* @return mixed
34+
*/
35+
public function run()
36+
{
37+
return $this->command->execute();
38+
}
39+
}

Behavioral/Command/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Command Design Pattern
2+
3+
## Intent
4+
5+
“Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.”
6+
7+
Excerpt From: Erich Gamma. “Design Patterns: Elements of Reusable Object-Oriented Software.”
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Behavioral\Command;
4+
5+
/**
6+
* Interface ReceiverInterface
7+
* @package PhpDesignPatterns\Behavioral\Command
8+
*/
9+
interface ReceiverInterface
10+
{
11+
/**
12+
* Execute a command.
13+
*
14+
* @param $command
15+
* @return string
16+
*/
17+
public function executeCommand($command);
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Behavioral\Command;
4+
5+
/**
6+
* Class SwitchButtonCommand
7+
* @package PhpDesignPatterns\Behavioral\Command
8+
*/
9+
class SwitchButtonCommand implements CommandInterface
10+
{
11+
/**
12+
* Instance of ReceiverInterface.
13+
*
14+
* @var ReceiverInterface
15+
*/
16+
private $control;
17+
18+
/**
19+
* Class constructor.
20+
*
21+
* @param ReceiverInterface $control
22+
*/
23+
public function __construct(ReceiverInterface $control)
24+
{
25+
$this->control = $control;
26+
}
27+
28+
/**
29+
* Execute the switch tv command.
30+
*
31+
* @return string
32+
*/
33+
public function execute()
34+
{
35+
return $this->control->executeCommand("Tv has been switched on.");
36+
}
37+
}

Behavioral/Command/TvControl.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Behavioral\Command;
4+
5+
/**
6+
* Class TvControl
7+
* @package PhpDesignPatterns\Behavioral\Command
8+
*/
9+
class TvControl implements ReceiverInterface
10+
{
11+
/**
12+
* Execute the command in input.
13+
*
14+
* @param $command
15+
* @return string
16+
*/
17+
public function executeCommand($command)
18+
{
19+
/* Dummy implementation here. You would implement the logic to execute the command. */
20+
return $command;
21+
}
22+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Tests\Behavioral\Command;
4+
5+
use PhpDesignPatterns\Behavioral\Command\CommandInvoker;
6+
7+
/**
8+
* Class CommandInvokerTest
9+
* @package PhpDesignPatterns\Tests\Behavioral\Command
10+
*/
11+
class CommandInvokerTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* Testing the command.
15+
*/
16+
public function testCommand()
17+
{
18+
$command = $this->getMock('PhpDesignPatterns\\Behavioral\\Command\\CommandInterface', array('execute'));
19+
$command
20+
->expects($this->once())
21+
->method('execute')
22+
->will($this->returnValue($expected_output = 'Dummy command!'))
23+
;
24+
$invoker = new CommandInvoker;
25+
$invoker->setCommand($command);
26+
$this->assertEquals($expected_output, $invoker->run());
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Tests\Behavioral\Command;
4+
5+
use PhpDesignPatterns\Behavioral\Command\SwitchButtonCommand;
6+
7+
/**
8+
* Class SwitchButtonCommandTest
9+
* @package PhpDesignPatterns\Tests\Behavioral\Command
10+
*/
11+
class SwitchButtonCommandTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* Testing execute.
15+
*/
16+
public function testExecuteCommand()
17+
{
18+
$receiver = $this->getMock("\\PhpDesignPatterns\\Behavioral\\Command\\ReceiverInterface", array("executeCommand"));
19+
$receiver
20+
->expects($this->once())
21+
->method("executeCommand")
22+
->with("Tv has been switched on.")
23+
->will($this->returnValue("Tv has been switched on."))
24+
;
25+
$command = new SwitchButtonCommand($receiver);
26+
$this->assertEquals("Tv has been switched on.", $command->execute());
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace PhpDesignPatterns\Tests\Behavioral\Command;
4+
5+
use PhpDesignPatterns\Behavioral\Command\TvControl;
6+
7+
/**
8+
* Class TvControlTest
9+
* @package PhpDesignPatterns\Tests\Behavioral\Command
10+
*/
11+
class TvControlTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* Testing execute.
15+
*/
16+
public function testExecuteCommand()
17+
{
18+
$control = new TvControl;
19+
$expected_command = "dummy command.";
20+
$this->assertEquals($expected_command, $control->executeCommand($expected_command));
21+
}
22+
}

0 commit comments

Comments
 (0)