mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-04 14:07:25 +02:00
PHP7 AbstractFactory
This commit is contained in:
@@ -3,38 +3,10 @@
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* class AbstractFactory.
|
||||
*
|
||||
* Sometimes also known as "Kit" in a GUI libraries.
|
||||
*
|
||||
* This design pattern implements the Dependency Inversion Principle since
|
||||
* it is the concrete subclass which creates concrete components.
|
||||
*
|
||||
* In this case, the abstract factory is a contract for creating some components
|
||||
* for the web. There are two components : Text and Picture. There are two ways
|
||||
* of rendering : HTML or JSON.
|
||||
*
|
||||
* Therefore 4 concrete classes, but the client just needs to know this contract
|
||||
* to build a correct HTTP response (for a HTML page or for an AJAX request).
|
||||
* for the web. There are two ways of rendering text: HTML and JSON
|
||||
*/
|
||||
abstract class AbstractFactory
|
||||
{
|
||||
/**
|
||||
* Creates a text component.
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return Text
|
||||
*/
|
||||
abstract public function createText($content);
|
||||
|
||||
/**
|
||||
* Creates a picture component.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
*
|
||||
* @return Picture
|
||||
*/
|
||||
abstract public function createPicture($path, $name = '');
|
||||
abstract public function createText(string $content): Text;
|
||||
}
|
||||
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory\Html;
|
||||
|
||||
use DesignPatterns\Creational\AbstractFactory\Picture as BasePicture;
|
||||
|
||||
class Picture extends BasePicture
|
||||
{
|
||||
/**
|
||||
* some crude rendering from HTML output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return sprintf('<img src="%s" title="%s"/>', $this->path, $this->name);
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory\Html;
|
||||
|
||||
use DesignPatterns\Creational\AbstractFactory\Text as BaseText;
|
||||
|
||||
class Text extends BaseText
|
||||
{
|
||||
/**
|
||||
* some crude rendering from HTML output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return '<div>'.htmlspecialchars($this->text).'</div>';
|
||||
}
|
||||
}
|
@@ -4,28 +4,8 @@ namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class HtmlFactory extends AbstractFactory
|
||||
{
|
||||
/**
|
||||
* Creates a picture component.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
*
|
||||
* @return Html\Picture|Picture
|
||||
*/
|
||||
public function createPicture($path, $name = '')
|
||||
public function createText(string $content): Text
|
||||
{
|
||||
return new Html\Picture($path, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a text component.
|
||||
*
|
||||
* @param string $content
|
||||
*
|
||||
* @return Html\Text|Text
|
||||
*/
|
||||
public function createText($content)
|
||||
{
|
||||
return new Html\Text($content);
|
||||
return new HtmlText($content);
|
||||
}
|
||||
}
|
||||
|
8
Creational/AbstractFactory/HtmlText.php
Normal file
8
Creational/AbstractFactory/HtmlText.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class HtmlText extends Text
|
||||
{
|
||||
// do something here
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory\Json;
|
||||
|
||||
use DesignPatterns\Creational\AbstractFactory\Picture as BasePicture;
|
||||
|
||||
class Picture extends BasePicture
|
||||
{
|
||||
/**
|
||||
* some crude rendering from JSON output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return json_encode(array('title' => $this->name, 'path' => $this->path));
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory\Json;
|
||||
|
||||
use DesignPatterns\Creational\AbstractFactory\Text as BaseText;
|
||||
|
||||
class Text extends BaseText
|
||||
{
|
||||
/**
|
||||
* some crude rendering from JSON output.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render()
|
||||
{
|
||||
return json_encode(array('content' => $this->text));
|
||||
}
|
||||
}
|
@@ -4,28 +4,8 @@ namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class JsonFactory extends AbstractFactory
|
||||
{
|
||||
/**
|
||||
* Creates a picture component.
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
*
|
||||
* @return Json\Picture|Picture
|
||||
*/
|
||||
public function createPicture($path, $name = '')
|
||||
public function createText(string $content): Text
|
||||
{
|
||||
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);
|
||||
return new JsonText($content);
|
||||
}
|
||||
}
|
||||
|
8
Creational/AbstractFactory/JsonText.php
Normal file
8
Creational/AbstractFactory/JsonText.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
class JsonText extends Text
|
||||
{
|
||||
// do something here
|
||||
}
|
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
/**
|
||||
* Interface MediaInterface.
|
||||
*
|
||||
* This contract is not part of the pattern, in general case, each component
|
||||
* are not related
|
||||
*/
|
||||
interface MediaInterface
|
||||
{
|
||||
/**
|
||||
* some crude rendering from JSON or html output (depended on concrete class).
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function render();
|
||||
}
|
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
abstract class Picture implements MediaInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $name;
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct($path, $name = '')
|
||||
{
|
||||
$this->name = (string) $name;
|
||||
$this->path = (string) $path;
|
||||
}
|
||||
}
|
@@ -39,45 +39,21 @@ HtmlFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
MediaInterface.php
|
||||
|
||||
.. literalinclude:: MediaInterface.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Picture.php
|
||||
|
||||
.. literalinclude:: Picture.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Text.php
|
||||
|
||||
.. literalinclude:: Text.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Json/Picture.php
|
||||
JsonText.php
|
||||
|
||||
.. literalinclude:: Json/Picture.php
|
||||
.. literalinclude:: JsonText.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Json/Text.php
|
||||
HtmlText.php
|
||||
|
||||
.. literalinclude:: Json/Text.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Html/Picture.php
|
||||
|
||||
.. literalinclude:: Html/Picture.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Html/Text.php
|
||||
|
||||
.. literalinclude:: Html/Text.php
|
||||
.. literalinclude:: HtmlText.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
|
@@ -2,43 +2,24 @@
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory\Tests;
|
||||
|
||||
use DesignPatterns\Creational\AbstractFactory\AbstractFactory;
|
||||
use DesignPatterns\Creational\AbstractFactory\HtmlFactory;
|
||||
use DesignPatterns\Creational\AbstractFactory\JsonFactory;
|
||||
|
||||
/**
|
||||
* AbstractFactoryTest tests concrete factories.
|
||||
*/
|
||||
class AbstractFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function getFactories()
|
||||
public function testCanCreateHtmlText()
|
||||
{
|
||||
return array(
|
||||
array(new JsonFactory()),
|
||||
array(new HtmlFactory()),
|
||||
);
|
||||
$factory = new HtmlFactory();
|
||||
$text = $factory->createText('foobar');
|
||||
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\AbstractFactory\HtmlText', $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the client of factories. Note that the client does not
|
||||
* care which factory is given to him, he can create any component he
|
||||
* wants and render how he wants.
|
||||
*
|
||||
* @dataProvider getFactories
|
||||
*/
|
||||
public function testComponentCreation(AbstractFactory $factory)
|
||||
public function testCanCreateJsonText()
|
||||
{
|
||||
$article = array(
|
||||
$factory->createText('Lorem Ipsum'),
|
||||
$factory->createPicture('/image.jpg', 'caption'),
|
||||
$factory->createText('footnotes'),
|
||||
);
|
||||
$factory = new JsonFactory();
|
||||
$text = $factory->createText('foobar');
|
||||
|
||||
$this->assertContainsOnly('DesignPatterns\Creational\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
|
||||
*/
|
||||
$this->assertInstanceOf('DesignPatterns\Creational\AbstractFactory\JsonText', $text);
|
||||
}
|
||||
}
|
||||
|
@@ -2,18 +2,15 @@
|
||||
|
||||
namespace DesignPatterns\Creational\AbstractFactory;
|
||||
|
||||
abstract class Text implements MediaInterface
|
||||
abstract class Text
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $text;
|
||||
private $text;
|
||||
|
||||
/**
|
||||
* @param string $text
|
||||
*/
|
||||
public function __construct($text)
|
||||
public function __construct(string $text)
|
||||
{
|
||||
$this->text = (string) $text;
|
||||
$this->text = $text;
|
||||
}
|
||||
}
|
||||
|
@@ -3,35 +3,48 @@
|
||||
<ID>PHP</ID>
|
||||
<OriginalElement>\DesignPatterns\Creational\AbstractFactory\AbstractFactory</OriginalElement>
|
||||
<nodes>
|
||||
<node x="281.0" y="228.0">\DesignPatterns\Creational\AbstractFactory\Html\Text</node>
|
||||
<node x="150.0" y="229.0">\DesignPatterns\Creational\AbstractFactory\Html\Picture</node>
|
||||
<node x="223.0" y="117.0">\DesignPatterns\Creational\AbstractFactory\HtmlFactory</node>
|
||||
<node x="0.0" y="117.0">\DesignPatterns\Creational\AbstractFactory\JsonFactory</node>
|
||||
<node x="126.0" y="0.0">\DesignPatterns\Creational\AbstractFactory\AbstractFactory</node>
|
||||
<node x="0.0" y="229.0">\DesignPatterns\Creational\AbstractFactory\MediaInterface</node>
|
||||
<node x="214.0" y="101.0">\DesignPatterns\Creational\AbstractFactory\JsonFactory</node>
|
||||
<node x="107.0" y="0.0">\DesignPatterns\Creational\AbstractFactory\AbstractFactory</node>
|
||||
<node x="0.0" y="101.0">\DesignPatterns\Creational\AbstractFactory\HtmlFactory</node>
|
||||
<node x="111.0" y="298.0">\DesignPatterns\Creational\AbstractFactory\JsonText</node>
|
||||
<node x="0.0" y="298.0">\DesignPatterns\Creational\AbstractFactory\HtmlText</node>
|
||||
<node x="51.5" y="197.0">\DesignPatterns\Creational\AbstractFactory\Text</node>
|
||||
</nodes>
|
||||
<notes />
|
||||
<edges>
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\HtmlFactory" target="\DesignPatterns\Creational\AbstractFactory\AbstractFactory">
|
||||
<point x="0.0" y="-33.5" />
|
||||
<point x="324.5" y="92.0" />
|
||||
<point x="256.5" y="92.0" />
|
||||
<point x="43.5" y="33.5" />
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\HtmlText" target="\DesignPatterns\Creational\AbstractFactory\Text">
|
||||
<point x="0.0" y="-14.5" />
|
||||
<point x="45.5" y="273.0" />
|
||||
<point x="75.75" y="273.0" />
|
||||
<point x="-24.25" y="25.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\JsonText" target="\DesignPatterns\Creational\AbstractFactory\Text">
|
||||
<point x="0.0" y="-14.5" />
|
||||
<point x="154.5" y="273.0" />
|
||||
<point x="124.25" y="273.0" />
|
||||
<point x="24.25" y="25.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\JsonFactory" target="\DesignPatterns\Creational\AbstractFactory\AbstractFactory">
|
||||
<point x="0.0" y="-33.5" />
|
||||
<point x="101.5" y="92.0" />
|
||||
<point x="169.5" y="92.0" />
|
||||
<point x="-43.5" y="33.5" />
|
||||
<point x="0.0" y="-25.5" />
|
||||
<point x="311.0" y="76.0" />
|
||||
<point x="252.5" y="76.0" />
|
||||
<point x="48.5" y="25.5" />
|
||||
</edge>
|
||||
<edge source="\DesignPatterns\Creational\AbstractFactory\HtmlFactory" target="\DesignPatterns\Creational\AbstractFactory\AbstractFactory">
|
||||
<point x="0.0" y="-25.5" />
|
||||
<point x="97.0" y="76.0" />
|
||||
<point x="155.5" y="76.0" />
|
||||
<point x="-48.5" y="25.5" />
|
||||
</edge>
|
||||
</edges>
|
||||
<settings layout="Hierarchic Group" zoom="1.0" x="213.0" y="138.0" />
|
||||
<SelectedNodes />
|
||||
<settings layout="Hierarchic Group" zoom="1.0" x="117.0" y="130.5" />
|
||||
<SelectedNodes>
|
||||
<node>\DesignPatterns\Creational\AbstractFactory\AbstractFactory</node>
|
||||
</SelectedNodes>
|
||||
<Categories>
|
||||
<Category>Fields</Category>
|
||||
<Category>Constants</Category>
|
||||
<Category>Constructors</Category>
|
||||
<Category>Methods</Category>
|
||||
<Category>Constants</Category>
|
||||
<Category>Fields</Category>
|
||||
</Categories>
|
||||
<VISIBILITY>private</VISIBILITY>
|
||||
</Diagram>
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 33 KiB |
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 50 KiB After Width: | Height: | Size: 90 KiB |
Reference in New Issue
Block a user