Merge branch 'adapter-working-example'

This commit is contained in:
Trismegiste
2013-05-11 20:57:21 +02:00
7 changed files with 171 additions and 37 deletions

View File

@@ -1,37 +0,0 @@
<?php
namespace DesignPatterns;
/**
* adapter pattern
*
* Purpose:
* to link two systems, that have different interfaces. An adapter defines interfaces that are equal for all linked
* systems and wrap functionality
*
* Examples:
* - different databases have the same interface to communicate although the underlying systems act differently
*
* this is a VERY basic example which won't work at all!
*/
interface DatabaseAdapter
{
public function getTables();
}
class MySQL implements DatabaseAdapter
{
public function getTables()
{
return $this->select('SHOW TABLES');
}
}
class SQLite implements DatabaseAdapter
{
public function getTables()
{
return system('sqlite --list-tables');
}
}

25
Adapter/Book.php Normal file
View File

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

View File

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

View File

@@ -0,0 +1,18 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Adapter;
/**
* ElecBookInterface is a contract for an electronic book
*/
interface ElecBookInterface
{
function pressNext();
function pressStart();
}

27
Adapter/Kindle.php Normal file
View File

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

View File

@@ -0,0 +1,18 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Adapter;
/**
* PaperBookInterface is a contract for a book
*/
interface PaperBookInterface
{
function turnPage();
function open();
}

View File

@@ -0,0 +1,42 @@
<?php
/*
* DesignPatternPHP
*/
namespace DesignPatterns\Tests\Adapter;
use DesignPatterns\Adapter\ElecBookAdapter;
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
{
public function getBook()
{
return array(
array(new Book()),
// we build a "wrapped" electronic book in the adapter
array(new ElecBookAdapter(new Kindle()))
);
}
/**
* This client only knows paper book but surprise, surprise, the second book
* is in fact an electronic book.
*
* @dataProvider getBook
*/
public function test_I_am_an_old_Client(PaperBookInterface $book)
{
$book->open();
$book->turnPage();
}
}