-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmediator.php
127 lines (113 loc) · 4.06 KB
/
mediator.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
class BookMediator {
private $authorObject;
private $titleObject;
function __construct($author_in, $title_in) {
$this->authorObject = new BookAuthorColleague($author_in,$this);
$this->titleObject = new BookTitleColleague($title_in,$this);
}
function getAuthor() {return $this->authorObject;}
function getTitle() {return $this->titleObject;}
// when title or author change case, this makes sure the other
// stays in sync
function change(BookColleague $changingClassIn) {
if ($changingClassIn instanceof BookAuthorColleague) {
if ('upper' == $changingClassIn->getState()) {
if ('upper' != $this->getTitle()->getState()) {
$this->getTitle()->setTitleUpperCase();
}
} elseif ('lower' == $changingClassIn->getState()) {
if ('lower' != $this->getTitle()->getState()) {
$this->getTitle()->setTitleLowerCase();
}
}
} elseif ($changingClassIn instanceof BookTitleColleague) {
if ('upper' == $changingClassIn->getState()) {
if ('upper' != $this->getAuthor()->getState()) {
$this->getAuthor()->setAuthorUpperCase();
}
} elseif ('lower' == $changingClassIn->getState()) {
if ('lower' != $this->getAuthor()->getState()) {
$this->getAuthor()->setAuthorLowerCase();
}
}
}
}
}
abstract class BookColleague {
private $mediator;
function __construct($mediator_in) {
$this->mediator = $mediator_in;
}
function getMediator() {return $this->mediator;}
function changed($changingClassIn) {
getMediator()->titleChanged($changingClassIn);
}
}
class BookAuthorColleague extends BookColleague {
private $author;
private $state;
function __construct($author_in, $mediator_in) {
$this->author = $author_in;
parent::__construct($mediator_in);
}
function getAuthor() {return $this->author;}
function setAuthor($author_in) {$this->author = $author_in;}
function getState() {return $this->state;}
function setState($state_in) {$this->state = $state_in;}
function setAuthorUpperCase() {
$this->setAuthor(strtoupper($this->getAuthor()));
$this->setState('upper');
$this->getMediator()->change($this);
}
function setAuthorLowerCase() {
$this->setAuthor(strtolower($this->getAuthor()));
$this->setState('lower');
$this->getMediator()->change($this);
}
}
class BookTitleColleague extends BookColleague {
private $title;
private $state;
function __construct($title_in, $mediator_in) {
$this->title = $title_in;
parent::__construct($mediator_in);
}
function getTitle() {return $this->title;}
function setTitle($title_in) {$this->title = $title_in;}
function getState() {return $this->state;}
function setState($state_in) {$this->state = $state_in;}
function setTitleUpperCase() {
$this->setTitle(strtoupper($this->getTitle()));
$this->setState('upper');
$this->getMediator()->change($this);
}
function setTitleLowerCase() {
$this->setTitle(strtolower($this->getTitle()));
$this->setState('lower');
$this->getMediator()->change($this);
}
}
writeln('BEGIN TESTING MEDIATOR PATTERN');
writeln('');
$mediator = new BookMediator('Gamma, Helm, Johnson, and Vlissides', 'Design Patterns');
$author = $mediator->getAuthor();
$title = $mediator->getTitle();
writeln('Original Author and Title: ');
writeln('author: ' . $author->getAuthor());
writeln('title: ' . $title->getTitle());
writeln('');
$author->setAuthorLowerCase();
writeln('After Author set to Lower Case: ');
writeln('author: ' . $author->getAuthor());
writeln('title: ' . $title->getTitle());
writeln('');
$title->setTitleUpperCase();
writeln('After Title set to Upper Case: ');
writeln('author: ' . $author->getAuthor());
writeln('title: ' . $title->getTitle());
writeln('');
writeln('END TESTING MEDIATOR PATTERN');
function writeln($line_in) {
echo $line_in.'<br/>';
}