start a restructure

This commit is contained in:
Antonio Spinelli
2014-03-21 18:03:44 -03:00
parent b0b0d4a1a4
commit e59d70a0ac
180 changed files with 21 additions and 16 deletions

View File

@@ -0,0 +1,25 @@
<?php
namespace DesignPatterns\Adapter;
/**
* Book is a concrete and standard paper book
*/
class Book implements PaperBookInterface
{
/**
* {@inheritdoc}
*/
public function open()
{
}
/**
* {@inheritdoc}
*/
public function turnPage()
{
}
}

View File

@@ -0,0 +1,43 @@
<?php
namespace DesignPatterns\Adapter;
/**
* EBookAdapter is an adapter to fit an e-book like a paper book
*
* This is the adapter here. Notice it implements PaperBookInterface,
* therefore you don't have to change the code of the client which using paper book.
*/
class EBookAdapter implements PaperBookInterface
{
/**
* @var EBookInterface
*/
protected $eBook;
/**
* Notice the constructor, it "wraps" an electronic book
*
* @param EBookInterface $ebook
*/
public function __construct(EBookInterface $ebook)
{
$this->eBook = $ebook;
}
/**
* This class makes the proper translation from one interface to another
*/
public function open()
{
$this->eBook->pressStart();
}
/**
* turns pages
*/
public function turnPage()
{
$this->eBook->pressNext();
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace DesignPatterns\Adapter;
/**
* EBookInterface is a contract for an electronic book
*/
interface EBookInterface
{
/**
* go to next page
*
* @return mixed
*/
public function pressNext();
/**
* start the book
*
* @return mixed
*/
public function pressStart();
}

View File

@@ -0,0 +1,25 @@
<?php
namespace DesignPatterns\Adapter;
/**
* Kindle is a concrete electronic book
*/
class Kindle implements EBookInterface
{
/**
* {@inheritdoc}
*/
public function pressNext()
{
}
/**
* {@inheritdoc}
*/
public function pressStart()
{
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace DesignPatterns\Adapter;
/**
* PaperBookInterface is a contract for a book
*/
interface PaperBookInterface
{
/**
* method to turn pages
*
* @return mixed
*/
public function turnPage();
/**
* method to open the book
*
* @return mixed
*/
public function open();
}

View File

@@ -0,0 +1,10 @@
# Adapter / Wrapper
## Purpose
To translate one interface for a class into a compatible interface. An adapter allows classes to work together that normally could not because of incompatible interfaces by providing it's interface to clients while using the original interface.
## Examples
* DB Client libraries adapter
* using multiple different webservices and adapters normalize data so that the outcome is the same for all

View File

@@ -0,0 +1,41 @@
<?php
namespace DesignPatterns\Tests\Adapter;
use DesignPatterns\Adapter\EBookAdapter;
use DesignPatterns\Adapter\Kindle;
use DesignPatterns\Adapter\PaperBookInterface;
use DesignPatterns\Adapter\Book;
/**
* AdapterTest shows the use of an adapted e-book that behave like a book
* You don't have to change the code of your client
*/
class AdapterTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function getBook()
{
return array(
array(new Book()),
// we build a "wrapped" electronic book in the adapter
array(new EBookAdapter(new Kindle()))
);
}
/**
* This client only knows paper book but surprise, surprise, the second book
* is in fact an electronic book, but both work the same way
*
* @param PaperBookInterface $book
*
* @dataProvider getBook
*/
public function test_I_am_an_old_Client(PaperBookInterface $book)
{
$book->open();
$book->turnPage();
}
}