mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 20:20:15 +02:00
cs AbstractFactory
This commit is contained in:
@@ -28,6 +28,7 @@ abstract class AbstractFactory
|
||||
* Creates a text component
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return Text
|
||||
*/
|
||||
abstract public function createText($content);
|
||||
@@ -37,6 +38,7 @@ abstract class AbstractFactory
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
*
|
||||
* @return Picture
|
||||
*/
|
||||
abstract public function createPicture($path, $name = '');
|
||||
|
@@ -11,7 +11,6 @@ use DesignPatterns\AbstractFactory\Picture as BasePicture;
|
||||
*/
|
||||
class Picture extends BasePicture
|
||||
{
|
||||
|
||||
/**
|
||||
* some crude rendering from HTML output
|
||||
*
|
||||
@@ -19,7 +18,6 @@ class Picture extends BasePicture
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return sprintf('<img src="%s" title="$s"/>', $this->_path, $this->_name);
|
||||
return sprintf('<img src="%s" title="$s"/>', $this->path, $this->name);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -11,7 +11,6 @@ use DesignPatterns\AbstractFactory\Text as BaseText;
|
||||
*/
|
||||
class Text extends BaseText
|
||||
{
|
||||
|
||||
/**
|
||||
* some crude rendering from HTML output
|
||||
*
|
||||
@@ -19,7 +18,6 @@ class Text extends BaseText
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return '<div>' . htmlspecialchars($this->_text) . '</div>';
|
||||
return '<div>' . htmlspecialchars($this->text) . '</div>';
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -9,12 +9,12 @@ namespace DesignPatterns\AbstractFactory;
|
||||
*/
|
||||
class HtmlFactory extends AbstractFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* Creates a picture component
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
*
|
||||
* @return Html\Picture|Picture
|
||||
*/
|
||||
public function createPicture($path, $name = '')
|
||||
@@ -26,11 +26,11 @@ class HtmlFactory extends AbstractFactory
|
||||
* Creates a text component
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return Html\Text|Text
|
||||
*/
|
||||
public function createText($content)
|
||||
{
|
||||
return new Html\Text($content);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -11,7 +11,6 @@ use DesignPatterns\AbstractFactory\Picture as BasePicture;
|
||||
*/
|
||||
class Picture extends BasePicture
|
||||
{
|
||||
|
||||
/**
|
||||
* some crude rendering from JSON output
|
||||
*
|
||||
@@ -19,7 +18,6 @@ class Picture extends BasePicture
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return json_encode(array('title' => $this->_name, 'path' => $this->_path));
|
||||
return json_encode(array('title' => $this->name, 'path' => $this->path));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -11,7 +11,6 @@ use DesignPatterns\AbstractFactory\Text as BaseText;
|
||||
*/
|
||||
class Text extends BaseText
|
||||
{
|
||||
|
||||
/**
|
||||
* some crude rendering from JSON output
|
||||
*
|
||||
@@ -19,7 +18,6 @@ class Text extends BaseText
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return json_encode(array('content' => $this->_text));
|
||||
return json_encode(array('content' => $this->text));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ class JsonFactory extends AbstractFactory
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
*
|
||||
* @return Json\Picture|Picture
|
||||
*/
|
||||
public function createPicture($path, $name = '')
|
||||
@@ -23,16 +24,15 @@ class JsonFactory extends AbstractFactory
|
||||
return new Json\Picture($path, $name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a text component
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return Json\Text|Text
|
||||
*/
|
||||
public function createText($content)
|
||||
{
|
||||
return new Json\Text($content);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -3,12 +3,12 @@
|
||||
namespace DesignPatterns\AbstractFactory;
|
||||
|
||||
/**
|
||||
* Interface Media
|
||||
* Interface MediaInterface
|
||||
*
|
||||
* This contract is not part of the pattern, in general case, each component
|
||||
* are not related
|
||||
*/
|
||||
interface Media
|
||||
interface MediaInterface
|
||||
{
|
||||
|
||||
/**
|
@@ -5,7 +5,7 @@ namespace DesignPatterns\AbstractFactory;
|
||||
/**
|
||||
* Class Picture
|
||||
*/
|
||||
abstract class Picture implements Media
|
||||
abstract class Picture implements MediaInterface
|
||||
{
|
||||
|
||||
/**
|
||||
|
@@ -5,7 +5,7 @@ namespace DesignPatterns\AbstractFactory;
|
||||
/**
|
||||
* Class Text
|
||||
*/
|
||||
abstract class Text implements Media
|
||||
abstract class Text implements MediaInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
|
@@ -7,20 +7,22 @@
|
||||
namespace DesignPatterns\Adapter;
|
||||
|
||||
/**
|
||||
* ElecBookAdapter is an adapter to fit an e-book like a paper book
|
||||
* EBookAdapter 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
|
||||
class EBookAdapter implements PaperBookInterface
|
||||
{
|
||||
|
||||
protected $eBook;
|
||||
|
||||
/**
|
||||
* Notice the constructor, it "wraps" an electronic book
|
||||
*
|
||||
* @param EBookInterface $ebook
|
||||
*/
|
||||
public function __construct(ElecBookInterface $ebook)
|
||||
public function __construct(EBookInterface $ebook)
|
||||
{
|
||||
$this->eBook = $ebook;
|
||||
}
|
||||
@@ -33,9 +35,11 @@ class ElecBookAdapter implements PaperBookInterface
|
||||
$this->eBook->pressStart();
|
||||
}
|
||||
|
||||
/**
|
||||
* turns pages
|
||||
*/
|
||||
public function turnPage()
|
||||
{
|
||||
$this->eBook->pressNext();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
14
Adapter/EBookInterface.php
Normal file
14
Adapter/EBookInterface.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Adapter;
|
||||
|
||||
/**
|
||||
* EBookInterface is a contract for an electronic book
|
||||
*/
|
||||
interface EBookInterface
|
||||
{
|
||||
|
||||
function pressNext();
|
||||
|
||||
function pressStart();
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Adapter;
|
||||
|
||||
/**
|
||||
* ElecBookInterface is a contract for an electronic book
|
||||
*/
|
||||
interface ElecBookInterface
|
||||
{
|
||||
|
||||
function pressNext();
|
||||
|
||||
function pressStart();
|
||||
}
|
@@ -25,7 +25,6 @@ namespace DesignPatterns\Command;
|
||||
*/
|
||||
interface Command
|
||||
{
|
||||
|
||||
/**
|
||||
* this is the most important method in the Command pattern,
|
||||
* The Receiver goes in the constructor.
|
||||
|
@@ -1,9 +1,5 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Tests\AbstractFactory;
|
||||
|
||||
use DesignPatterns\AbstractFactory\AbstractFactory;
|
||||
@@ -15,7 +11,6 @@ use DesignPatterns\AbstractFactory\JsonFactory;
|
||||
*/
|
||||
class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function getFactories()
|
||||
{
|
||||
return array(
|
||||
@@ -39,12 +34,11 @@ class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
$factory->createText('footnotes')
|
||||
);
|
||||
|
||||
$this->assertContainsOnly('DesignPatterns\AbstractFactory\Media', $article);
|
||||
$this->assertContainsOnly('DesignPatterns\AbstractFactory\MediaInterface', $article);
|
||||
|
||||
/* this is the time to look at the Builder pattern. This pattern
|
||||
* helps you to create complex object like that article above with
|
||||
* a given Abstract Factory
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user