-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcommand.php
86 lines (75 loc) · 2.22 KB
/
command.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
class BookCommandee {
private $author;
private $title;
function __construct($title_in, $author_in) {
$this->setAuthor($author_in);
$this->setTitle($title_in);
}
function getAuthor() {
return $this->author;
}
function setAuthor($author_in) {
$this->author = $author_in;
}
function getTitle() {
return $this->title;
}
function setTitle($title_in) {
$this->title = $title_in;
}
function setStarsOn() {
$this->setAuthor(Str_replace(' ','*',$this->getAuthor()));
$this->setTitle(Str_replace(' ','*',$this->getTitle()));
}
function setStarsOff() {
$this->setAuthor(Str_replace('*',' ',$this->getAuthor()));
$this->setTitle(Str_replace('*',' ',$this->getTitle()));
}
function getAuthorAndTitle() {
return $this->getTitle().' by '.$this->getAuthor();
}
}
abstract class BookCommand {
protected $bookCommandee;
function __construct($bookCommandee_in) {
$this->bookCommandee = $bookCommandee_in;
}
abstract function execute();
}
class BookStarsOnCommand extends BookCommand {
function execute() {
$this->bookCommandee->setStarsOn();
}
}
class BookStarsOffCommand extends BookCommand {
function execute() {
$this->bookCommandee->setStarsOff();
}
}
writeln('BEGIN TESTING COMMAND PATTERN');
writeln('');
$book = new BookCommandee('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');
writeln('book after creation: ');
writeln($book->getAuthorAndTitle());
writeln('');
$starsOn = new BookStarsOnCommand($book);
callCommand($starsOn);
writeln('book after stars on: ');
writeln($book->getAuthorAndTitle());
writeln('');
$starsOff = new BookStarsOffCommand($book);
callCommand($starsOff);
writeln('book after stars off: ');
writeln($book->getAuthorAndTitle());
writeln('');
writeln('END TESTING COMMAND PATTERN');
// the callCommand function demonstrates that a specified
// function in BookCommandee can be executed with only
// an instance of BookCommand.
function callCommand(BookCommand $bookCommand_in) {
$bookCommand_in->execute();
}
function writeln($line_in) {
echo $line_in."<br/>";
}