mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-09-25 05:51:46 +02:00
start a restructure
This commit is contained in:
25
Structural/Adapter/Book.php
Normal file
25
Structural/Adapter/Book.php
Normal 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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
43
Structural/Adapter/EBookAdapter.php
Normal file
43
Structural/Adapter/EBookAdapter.php
Normal 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();
|
||||
}
|
||||
}
|
23
Structural/Adapter/EBookInterface.php
Normal file
23
Structural/Adapter/EBookInterface.php
Normal 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();
|
||||
}
|
25
Structural/Adapter/Kindle.php
Normal file
25
Structural/Adapter/Kindle.php
Normal 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()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
23
Structural/Adapter/PaperBookInterface.php
Normal file
23
Structural/Adapter/PaperBookInterface.php
Normal 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();
|
||||
}
|
10
Structural/Adapter/README.md
Normal file
10
Structural/Adapter/README.md
Normal 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
|
41
Structural/Adapter/Test/AdapterTest.php
Normal file
41
Structural/Adapter/Test/AdapterTest.php
Normal 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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user