Skip to content

Latest commit

 

History

History
77 lines (52 loc) · 1.44 KB

File metadata and controls

77 lines (52 loc) · 1.44 KB

Purpose

The good point over the SimpleFactory is you can subclass it to implement different ways to create objects.

For simple cases, 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 SOLID principles.

It means the FactoryMethod class depends on abstractions, not concrete classes. This is the real trick compared to SimpleFactory or StaticFactory.

UML Diagram

Alt FactoryMethod UML Diagram

Code

You can also find this code on GitHub

Logger.php

.. literalinclude:: Logger.php
   :language: php
   :linenos:

StdoutLogger.php

.. literalinclude:: StdoutLogger.php
   :language: php
   :linenos:

FileLogger.php

.. literalinclude:: FileLogger.php
   :language: php
   :linenos:

LoggerFactory.php

.. literalinclude:: LoggerFactory.php
   :language: php
   :linenos:

StdoutLoggerFactory.php

.. literalinclude:: StdoutLoggerFactory.php
   :language: php
   :linenos:

FileLoggerFactory.php

.. literalinclude:: FileLoggerFactory.php
   :language: php
   :linenos:

Test

Tests/FactoryMethodTest.php

.. literalinclude:: Tests/FactoryMethodTest.php
   :language: php
   :linenos: