-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.php
More file actions
138 lines (112 loc) · 2.59 KB
/
decorator.php
File metadata and controls
138 lines (112 loc) · 2.59 KB
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
128
129
130
131
132
133
134
135
136
137
138
<?php
/**
* Este padrao vai melhorando as caracteristicas de um objeto
*/
/**
* Classe que representa um livro
*/
class Book
{
private $author;
private $title;
function __construct($title_in, $author_in)
{
$this->author = $author_in;
$this->title = $title_in;
}
function getAuthor()
{
return $this->author;
}
function getTitle()
{
return $this->title;
}
function getAuthorAndTitle()
{
return $this->getTitle() . ' de ' . $this->getAuthor();
}
}
/**
* Padrao decorator.
* Esta classe tem as funcoes essenciais do decorador de livros
*/
class BookTitleDecorator
{
protected $book;
protected $title;
public function __construct(Book $book_in)
{
$this->book = $book_in;
$this->resetTitle();
}
//doing this so original object is not altered
function resetTitle()
{
$this->title = $this->book->getTitle();
}
function showTitle()
{
return $this->title;
}
}
/**
* Padrao decorator.
* Este decora o titulo de um livro
*/
class BookTitleExclaimDecorator extends BookTitleDecorator
{
private $btd;
public function __construct(BookTitleDecorator $btd_in)
{
$this->btd = $btd_in;
}
function exclaimTitle()
{
$this->btd->title = "!" . $this->btd->title . "!";
}
}
/**
* Padrao decorator.
* Este decora o titulo de um livro
*/
class BookTitleStarDecorator extends BookTitleDecorator
{
private $btd;
public function __construct(BookTitleDecorator $btd_in)
{
$this->btd = $btd_in;
}
function starTitle()
{
$this->btd->title = Str_replace(" ", "*", $this->btd->title);
}
}
writeln('BEGIN TESTING DECORATOR PATTERN');
writeln('');
$meu_livro = new Book('THE BLACK BOOK', 'Adriano Waltrick');
$decorator = new BookTitleDecorator($meu_livro);
$starDecorator = new BookTitleStarDecorator($decorator);
$exclaimDecorator = new BookTitleExclaimDecorator($decorator);
writeln('Titulo Original do Livro : ');
writeln($decorator->showTitle());
writeln('');
writeln('Titulo Alterado pelo decorador 1 : ');
$exclaimDecorator->exclaimTitle();
$exclaimDecorator->exclaimTitle();
writeln($decorator->showTitle());
writeln('');
writeln('Titulo Alterado pelo decorador 2 : ');
$starDecorator->starTitle();
writeln($decorator->showTitle());
writeln('');
writeln('Titulo ao normal novamente: ');
writeln($decorator->resetTitle());
writeln($decorator->showTitle());
writeln('');
writeln('END TESTING DECORATOR PATTERN');
function writeln($line_in)
{
echo $line_in . "<br/>";
}
?>