Skip to content

Instantly share code, notes, and snippets.

@puiutucutu
Created September 29, 2015 18:24
Show Gist options
  • Save puiutucutu/ccdf95b5f0d0024a7149 to your computer and use it in GitHub Desktop.
Save puiutucutu/ccdf95b5f0d0024a7149 to your computer and use it in GitHub Desktop.
<?php
<?php
abstract class OnTheBookShelf
{
abstract function getBookInfo($previousBook);
abstract function getBookCount();
abstract function setBookCount($new_count);
abstract function addBook($oneBook);
abstract function removeBook($oneBook);
}
class OneBook extends OnTheBookShelf
{
private $title;
private $author;
function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
function getBookInfo($bookToGet)
{
if (1 == $bookToGet)
return $this->title." by ".$this->author;
return FALSE;
}
function getBookCount()
{
return 1;
}
function setBookCount($newCount)
{
return FALSE;
}
function addBook($oneBook)
{
return FALSE;
}
function removeBook($oneBook)
{
return FALSE;
}
}
class SeveralBooks extends OnTheBookShelf
{
private $oneBooks = array();
private $bookCount;
public function __construct()
{
$this->setBookCount(0);
}
public function getBookCount()
{
return $this->bookCount;
}
public function setBookCount($newCount)
{
$this->bookCount = $newCount;
}
public function getBookInfo($bookToGet) {
if ($bookToGet <= $this->bookCount)
return $this->oneBooks[$bookToGet]->getBookInfo(1);
else
return FALSE;
}
public function addBook($oneBook)
{
$this->setBookCount($this->getBookCount() + 1);
$this->oneBooks[$this->getBookCount()] = $oneBook;
return $this->getBookCount();
}
public function removeBook($oneBook)
{
$counter = 0;
while (++$counter <= $this->getBookCount()) {
if ($oneBook->getBookInfo(1) ==
$this->oneBooks[$counter]->getBookInfo(1)) {
for ($x = $counter; $x < $this->getBookCount(); $x++) {
$this->oneBooks[$x] = $this->oneBooks[$x + 1];
}
$this->setBookCount($this->getBookCount() - 1);
}
}
return $this->getBookCount();
}
public function renderArray()
{
// echo "<pre><code>";
return($this->oneBooks);
// echo "</pre></code>";
}
}
writeln("BEGIN TESTING COMPOSITE PATTERN");
writeln('');
$firstBook = new OneBook('Core PHP Programming, Third Edition', 'Atkinson and Suraski');
writeln('(after creating first book) oneBook info: ');
writeln($firstBook->getBookInfo(1));
writeln('');
$secondBook = new OneBook('PHP Bible', 'Converse and Park');
writeln('(after creating second book) oneBook info: ');
writeln($secondBook->getBookInfo(1));
writeln('');
$thirdBook = new OneBook('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');
writeln('(after creating third book) oneBook info: ');
writeln($thirdBook->getBookInfo(1));
writeln('');
$books = new SeveralBooks();
$booksCount = $books->addBook($firstBook);
writeln('(after adding firstBook to books) SeveralBooks info : ');
writeln($books->getBookInfo($booksCount));
writeln('');
$booksCount = $books->addBook($secondBook);
writeln('(after adding secondBook to books) SeveralBooks info : ');
writeln($books->getBookInfo($booksCount));
writeln('');
$booksCount = $books->addBook($thirdBook);
writeln('(after adding thirdBook to books) SeveralBooks info : ');
writeln($books->getBookInfo($booksCount));
writeln('');
$booksCount = $books->removeBook($firstBook);
writeln('(after removing firstBook from books) SeveralBooks count : ');
writeln($books->getBookCount());
writeln('');
writeln('(after removing firstBook from books) SeveralBooks info 1 : ');
writeln($books->getBookInfo(1));
writeln('');
writeln('(after removing firstBook from books) SeveralBooks info 2 : ');
writeln($books->getBookInfo(2));
writeln('');
writeln('END TESTING COMPOSITE PATTERN');
$array = $books->renderArray();
echo "<pre><code>";
print_r($array);
function writeln($line_in) {
echo $line_in."<br/>";
}
@puiutucutu
Copy link
Author

Pattern

In its most basic form, this pattern requires two classes both extending from the same abstract class. The first class stores information about the object (in this case, author and title of a book).

The second class stores instances of class one objects (in an array).

  • In addition, the second class also serves the role of providing an access layer to retrieve information stored within those instances of class one objects.

Notes

Slightly modified it to improve readability, but still need to add comments to demonstrate full understanding of pattern.

  • possibly the methods need to be re-arranged based on order / importance of being called

UML Diagram

image

From https://sourcemaking.com/design_patterns/composite

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment