mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 12:10:10 +02:00
execute read-the-docs.sh
This commit is contained in:
69
Creational/AbstractFactory/index.rst
Normal file
69
Creational/AbstractFactory/index.rst
Normal file
@@ -0,0 +1,69 @@
|
||||
Abstract Factory
|
||||
================
|
||||
|
||||
Purpose
|
||||
-------
|
||||
|
||||
To create series of related or dependent objects without specifying
|
||||
their concrete classes. Usually the created classes all implement the
|
||||
same interface. The client of the abstract factory does not care about
|
||||
how these objects are created, he just knows how they go together.
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt AbstractFactory UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Text.php
|
||||
|
||||
.. literalinclude:: Text.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
JsonFactory.php
|
||||
|
||||
.. literalinclude:: JsonFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
AbstractFactory.php
|
||||
|
||||
.. literalinclude:: AbstractFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
MediaInterface.php
|
||||
|
||||
.. literalinclude:: MediaInterface.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
HtmlFactory.php
|
||||
|
||||
.. literalinclude:: HtmlFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Picture.php
|
||||
|
||||
.. literalinclude:: Picture.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/AbstractFactoryTest.php
|
||||
|
||||
.. literalinclude:: Tests/AbstractFactoryTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/AbstractFactory
|
53
Creational/Builder/Parts/index.rst
Normal file
53
Creational/Builder/Parts/index.rst
Normal file
@@ -0,0 +1,53 @@
|
||||
Purpose
|
||||
=======
|
||||
|
||||
Examples
|
||||
========
|
||||
|
||||
-
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Wheel.php
|
||||
|
||||
.. literalinclude:: Wheel.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Door.php
|
||||
|
||||
.. literalinclude:: Door.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Car.php
|
||||
|
||||
.. literalinclude:: Car.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Bike.php
|
||||
|
||||
.. literalinclude:: Bike.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Vehicle.php
|
||||
|
||||
.. literalinclude:: Vehicle.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Engine.php
|
||||
|
||||
.. literalinclude:: Engine.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Builder/Parts
|
68
Creational/Builder/index.rst
Normal file
68
Creational/Builder/index.rst
Normal file
@@ -0,0 +1,68 @@
|
||||
Builder
|
||||
=======
|
||||
|
||||
Purpose
|
||||
-------
|
||||
|
||||
Builder is an interface that build parts of a complex object.
|
||||
|
||||
Sometimes, if the builder has a better knowledge of what it builds, this
|
||||
interface could be an abstract class with default methods (aka adapter).
|
||||
|
||||
If you have a complex inheritance tree for objects, it is logical to
|
||||
have a complex inheritance tree for builders too.
|
||||
|
||||
Note: Builders have often a fluent interface, see the mock builder of
|
||||
PHPUnit for example.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
- PHPUnit: Mock Builder
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt Builder UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
BuilderInterface.php
|
||||
|
||||
.. literalinclude:: BuilderInterface.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
CarBuilder.php
|
||||
|
||||
.. literalinclude:: CarBuilder.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Director.php
|
||||
|
||||
.. literalinclude:: Director.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
BikeBuilder.php
|
||||
|
||||
.. literalinclude:: BikeBuilder.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/DirectorTest.php
|
||||
|
||||
.. literalinclude:: Tests/DirectorTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Builder
|
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
|
40
Creational/Multiton/index.rst
Normal file
40
Creational/Multiton/index.rst
Normal file
@@ -0,0 +1,40 @@
|
||||
Multiton
|
||||
========
|
||||
|
||||
**THIS IS CONSIDERED TO BE AN ANTI-PATTERN! FOR BETTER TESTABILITY AND
|
||||
MAINTAINABILITY USE DEPENDENCY INJECTION!**
|
||||
|
||||
Purpose
|
||||
=======
|
||||
|
||||
To have only a list of named instances that are used, like a singleton
|
||||
but with n instances.
|
||||
|
||||
Examples
|
||||
========
|
||||
|
||||
- 2 DB Connectors, e.g. one for MySQL, the other for SQLite
|
||||
- multiple Loggers (one for debug messages, one for errors)
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt Multiton UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Multiton.php
|
||||
|
||||
.. literalinclude:: Multiton.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Multiton
|
69
Creational/Pool/index.rst
Normal file
69
Creational/Pool/index.rst
Normal file
@@ -0,0 +1,69 @@
|
||||
Pool
|
||||
====
|
||||
|
||||
The **object pool pattern** is a software creational design pattern that
|
||||
uses a set of initialized objects kept ready to use – a "pool" – rather
|
||||
than allocating and destroying them on demand. A client of the pool will
|
||||
request an object from the pool and perform operations on the returned
|
||||
object. When the client has finished, it returns the object, which is a
|
||||
specific type of factory object, to the pool rather than destroying it.
|
||||
|
||||
Object pooling can offer a significant performance boost in situations
|
||||
where the cost of initializing a class instance is high, the rate of
|
||||
instantiation of a class is high, and the number of instances in use at
|
||||
any one time is low. The pooled object is obtained in predictable time
|
||||
when creation of the new objects (especially over network) may take
|
||||
variable time.
|
||||
|
||||
However these benefits are mostly true for objects that are expensive
|
||||
with respect to time, such as database connections, socket connections,
|
||||
threads and large graphic objects like fonts or bitmaps. In certain
|
||||
situations, simple object pooling (that hold no external resources, but
|
||||
only occupy memory) may not be efficient and could decrease performance.
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt Pool UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Pool.php
|
||||
|
||||
.. literalinclude:: Pool.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Processor.php
|
||||
|
||||
.. literalinclude:: Processor.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Worker.php
|
||||
|
||||
.. literalinclude:: Worker.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/TestWorker.php
|
||||
|
||||
.. literalinclude:: Tests/TestWorker.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Tests/PoolTest.php
|
||||
|
||||
.. literalinclude:: Tests/PoolTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Pool
|
55
Creational/Prototype/index.rst
Normal file
55
Creational/Prototype/index.rst
Normal file
@@ -0,0 +1,55 @@
|
||||
Prototype
|
||||
=========
|
||||
|
||||
Purpose
|
||||
-------
|
||||
|
||||
To avoid the cost of creating objects the standard way (new Foo()) and
|
||||
instead create a prototype and clone it.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
- Large amounts of data (e.g. create 1,000,000 rows in a database at
|
||||
once via a ORM).
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt Prototype UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
BookPrototype.php
|
||||
|
||||
.. literalinclude:: BookPrototype.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
BarBookPrototype.php
|
||||
|
||||
.. literalinclude:: BarBookPrototype.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
index.php
|
||||
|
||||
.. literalinclude:: index.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
FooBookPrototype.php
|
||||
|
||||
.. literalinclude:: FooBookPrototype.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Prototype
|
60
Creational/SimpleFactory/index.rst
Normal file
60
Creational/SimpleFactory/index.rst
Normal file
@@ -0,0 +1,60 @@
|
||||
Simple Factory
|
||||
==============
|
||||
|
||||
Purpose
|
||||
-------
|
||||
|
||||
ConcreteFactory is a simple factory pattern.
|
||||
|
||||
It differs from the static factory because it is NOT static and as you
|
||||
know: static => global => evil!
|
||||
|
||||
Therefore, you can have multiple factories, differently parametrized,
|
||||
you can subclass it and you can mock-up it.
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt SimpleFactory UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
ConcreteFactory.php
|
||||
|
||||
.. literalinclude:: ConcreteFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
VehicleInterface.php
|
||||
|
||||
.. literalinclude:: VehicleInterface.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Scooter.php
|
||||
|
||||
.. literalinclude:: Scooter.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Bicycle.php
|
||||
|
||||
.. literalinclude:: Bicycle.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/SimpleFactoryTest.php
|
||||
|
||||
.. literalinclude:: Tests/SimpleFactoryTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/SimpleFactory
|
49
Creational/Singleton/index.rst
Normal file
49
Creational/Singleton/index.rst
Normal file
@@ -0,0 +1,49 @@
|
||||
Singleton
|
||||
=========
|
||||
|
||||
**THIS IS CONSIDERED TO BE AN ANTI-PATTERN! FOR BETTER TESTABILITY AND
|
||||
MAINTAINABILITY USE DEPENDENCY INJECTION!**
|
||||
|
||||
Purpose
|
||||
-------
|
||||
|
||||
To have only one instance of this object in the application that will
|
||||
handle all calls.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
- DB Connector
|
||||
- Logger (may also be a Multiton if there are many log files for
|
||||
several purposes)
|
||||
- Lock file for the application (there is only one in the filesystem
|
||||
...)
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt Singleton UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Singleton.php
|
||||
|
||||
.. literalinclude:: Singleton.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/SingletonTest.php
|
||||
|
||||
.. literalinclude:: Tests/SingletonTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/Singleton
|
64
Creational/StaticFactory/index.rst
Normal file
64
Creational/StaticFactory/index.rst
Normal file
@@ -0,0 +1,64 @@
|
||||
Static Factory
|
||||
==============
|
||||
|
||||
Purpose
|
||||
-------
|
||||
|
||||
Similar to the AbstractFactory, this pattern is used to create series of
|
||||
related or dependent objects. The difference between this and the
|
||||
abstract factory pattern is that the static factory pattern uses just
|
||||
one static method to create all types of objects it can create. It is
|
||||
usually named ``factory`` or ``build``.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
- Zend Framework: ``Zend_Cache_Backend`` or ``_Frontend`` use a factory
|
||||
method create cache backends or frontends
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt StaticFactory UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
StaticFactory.php
|
||||
|
||||
.. literalinclude:: StaticFactory.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
FormatterInterface.php
|
||||
|
||||
.. literalinclude:: FormatterInterface.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
FormatString.php
|
||||
|
||||
.. literalinclude:: FormatString.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
FormatNumber.php
|
||||
|
||||
.. literalinclude:: FormatNumber.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/StaticFactoryTest.php
|
||||
|
||||
.. literalinclude:: Tests/StaticFactoryTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/StaticFactory
|
36
Creational/index.rst
Normal file
36
Creational/index.rst
Normal file
@@ -0,0 +1,36 @@
|
||||
Creational
|
||||
==========
|
||||
|
||||
In software engineering, creational design patterns are design patterns
|
||||
that deal with object creation mechanisms, trying to create objects in a
|
||||
manner suitable to the situation. The basic form of object creation
|
||||
could result in design problems or added complexity to the design.
|
||||
Creational design patterns solve this problem by somehow controlling
|
||||
this object creation.
|
||||
|
||||
- `AbstractFactory <AbstractFactory>`__
|
||||
`:notebook: <http://en.wikipedia.org/wiki/Abstract_factory_pattern>`__
|
||||
- `Builder <Builder>`__
|
||||
`:notebook: <http://en.wikipedia.org/wiki/Builder_pattern>`__
|
||||
- `FactoryMethod <FactoryMethod>`__
|
||||
`:notebook: <http://en.wikipedia.org/wiki/Factory_method_pattern>`__
|
||||
- `Multiton <Multiton>`__ (is considered an anti-pattern! :no\_entry:)
|
||||
- `Pool <Pool>`__
|
||||
`:notebook: <http://en.wikipedia.org/wiki/Object_pool_pattern>`__
|
||||
- `Prototype <Prototype>`__
|
||||
`:notebook: <http://en.wikipedia.org/wiki/Prototype_pattern>`__
|
||||
- `SimpleFactory <SimpleFactory>`__
|
||||
- `Singleton <Singleton>`__
|
||||
`:notebook: <http://en.wikipedia.org/wiki/Singleton_pattern>`__ (is
|
||||
considered an anti-pattern! :no\_entry:)
|
||||
- `StaticFactory <StaticFactory>`__
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational
|
Reference in New Issue
Block a user