-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathflyweight.php
102 lines (90 loc) · 2.89 KB
/
flyweight.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
<?php
class FlyweightBook {
private $author;
private $title;
function __construct($author_in, $title_in) {
$this->author = $author_in;
$this->title = $title_in;
}
function getAuthor() {
return $this->author;
}
function getTitle() {
return $this->title;
}
}
class FlyweightFactory {
private $books = array();
function __construct() {
$this->books[1] = NULL;
$this->books[2] = NULL;
$this->books[3] = NULL;
}
function getBook($bookKey) {
if (NULL == $this->books[$bookKey]) {
$makeFunction = 'makeBook'.$bookKey;
$this->books[$bookKey] = $this->$makeFunction();
}
return $this->books[$bookKey];
}
//Sort of an long way to do this, but hopefully easy to follow.
//How you really want to make flyweights would depend on what
//your application needs. This, while a little clumbsy looking,
//does work well.
function makeBook1() {
$book = new FlyweightBook('Larry Truett','PHP For Cats');
return $book;
}
function makeBook2() {
$book = new FlyweightBook('Larry Truett','PHP For Dogs');
return $book;
}
function makeBook3() {
$book = new FlyweightBook('Larry Truett','PHP For Parakeets');
return $book;
}
}
class FlyweightBookShelf {
private $books = array();
function addBook($book) {
$this->books[] = $book;
}
function showBooks() {
$return_string = NULL;
foreach ($this->books as $book) {
$return_string .= 'title: '.$book->getAuthor().' author: '.$book->getTitle();
};
return $return_string;
}
}
writeln('BEGIN TESTING FLYWEIGHT PATTERN');
$flyweightFactory = new FlyweightFactory();
$flyweightBookShelf1 = new FlyweightBookShelf();
$flyweightBook1 = $flyweightFactory->getBook(1);
$flyweightBookShelf1->addBook($flyweightBook1);
$flyweightBook2 = $flyweightFactory->getBook(1);
$flyweightBookShelf1->addBook($flyweightBook2);
writeln('test 1 - show the two books are the same book');
if ($flyweightBook1 === $flyweightBook2) {
writeln('1 and 2 are the same');
} else {
writeln('1 and 2 are not the same');
}
writeln('');
writeln('test 2 - with one book on one self twice');
writeln($flyweightBookShelf1->showBooks());
writeln('');
$flyweightBookShelf2 = new FlyweightBookShelf();
$flyweightBook1 = $flyweightFactory->getBook(2);
$flyweightBookShelf2->addBook($flyweightBook1);
$flyweightBookShelf1->addBook($flyweightBook1);
writeln('test 3 - book shelf one');
writeln($flyweightBookShelf1->showBooks());
writeln('');
writeln('test 3 - book shelf two');
writeln($flyweightBookShelf2->showBooks());
writeln('');
writeln('END TESTING FLYWEIGHT PATTERN');
function writeln($line_in) {
echo $line_in."<br/>";
}