mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-06 06:57:25 +02:00
execute read-the-docs.sh
This commit is contained in:
82
Creational/FactoryMethod/index.rst
Normal file
82
Creational/FactoryMethod/index.rst
Normal file
@@ -0,0 +1,82 @@
|
||||
Factory Method
|
||||
==============
|
||||
|
||||
Purpose
|
||||
-------
|
||||
|
||||
The good point over the SimpleFactory is you can subclass it to
|
||||
implement different ways to create objects
|
||||
|
||||
For simple case, this abstract class could be just an interface
|
||||
|
||||
This pattern is a "real" Design Pattern because it achieves the
|
||||
"Dependency Inversion Principle" a.k.a the "D" in S.O.L.I.D principles.
|
||||
|
||||
It means the FactoryMethod class depends on abstractions, not concrete
|
||||
classes. This is the real trick compared to SimpleFactory or
|
||||
StaticFactory.
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt FactoryMethod UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Porsche.php
|
||||
|
||||
.. literalinclude:: Porsche.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
GermanFactory.php
|
||||
|
||||
.. literalinclude:: GermanFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
ItalianFactory.php
|
||||
|
||||
.. literalinclude:: ItalianFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
VehicleInterface.php
|
||||
|
||||
.. literalinclude:: VehicleInterface.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
FactoryMethod.php
|
||||
|
||||
.. literalinclude:: FactoryMethod.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Bicycle.php
|
||||
|
||||
.. literalinclude:: Bicycle.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Ferrari.php
|
||||
|
||||
.. literalinclude:: Ferrari.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/FactoryMethodTest.php
|
||||
|
||||
.. literalinclude:: Tests/FactoryMethodTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/FactoryMethod
|
Reference in New Issue
Block a user