Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions Homeworks/1-Creational-design-patterns/Answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
//Creational design patterns

abstract class DatabaseAbstractFactory
{
public $connection;
abstract public function connection();
abstract public function query();
}

class MysqlFactory extends DatabaseAbstractFactory
{
public function connection() {}

public function query() {}
}

class MSsqlFactory extends DatabaseAbstractFactory
{
public function connection() {}

public function query() {}
}

class MariaDBFactory extends DatabaseAbstractFactory
{
public function connection() {}

public function query() {}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions Homeworks/2-Structural-design-patterns/Answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// Structural design patterns

interface XMLInterface
{
public function getXML();
}

class XML implements XMLInterface
{
protected $xml;

public function getXML()
{
// return XML Format
}
}


interface JsonInterface
{
public function getJson();
}

class Json implements JsonInterface
{
protected $json;

public function getJson()
{
// return Json Format
}
}


class XMLAdapter implements JsonInterface
{
protected $xml;

public function __construct(XMLInterface $xml)
{
$this->xml = $xml;
}

public function getJson()
{
$xml = simplexml_load_string($this->xml->getXML());
return json_encode($xml);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions Homeworks/3-Behavioral-design-patterns/Answer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
// Behavioral design patterns

class Book {
private $id, $content;
public function __construct($id, $content)
{
$this->id = $id;
$this->content = $content;
}
public function getId()
{
return $this->id;
}
public function getContent()
{
return $this->content;
}
}

class BookList {
private $books = [];
private $bookCount = 0;

public function getBookCount()
{
return $this->bookCount;
}
public function getBook($bookNumber) {
return isset($this->books[$bookNumber]) ? $this->books[$bookNumber] : null;
}
public function addBook(Book $book)
{
$this->bookCount += 1;
$this->books[$this->bookCount] = $book;
return $this->bookCount;
}
public function removeBook(Book $book) {
$counter = 0;
while (++$counter <= $this->getBookCount()) {
if ($book->getId() == $this->books[$counter]->getId()) {
for ($i = $counter; $i < $this->getBookCount(); $i++) {
$this->books[$i] = $this->books[$i + 1];
}
$this->bookCount -= 1;
}
}
return $this->getBookCount();
}
}

class BookListIterator {
protected $bookList;
protected $currentBook = 0;

public function __construct(BookList $bookList)
{
$this->bookList = $bookList;
}
public function getCurrentBook()
{
return isset($this->bookList[$this->currentBook]) ? $this->bookList[$this->currentBook] : null;
}
public function hasNextBook()
{
return $this->bookList->getBookCount() > $this->currentBook;
}
public function getNextBook() {
return $this->hasNextBook() ? $this->bookList->getBook(++$this->currentBook) : null;
}
}

class BookListReverseIterator extends BookListIterator {
public function __construct(BookList $bookList)
{
$this->bookList = $bookList;
$this->currentBook = $this->bookList->getBookCount() + 1;
}
public function hasNextBook() {
return 1 < $this->currentBook;
}
public function getNextBook() {
return $this->hasNextBook() ? $this->bookList->getBook(--$this->currentBook) : null;
}
}