示例#1
0
    {
        $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);