Skip to content

Files

Latest commit

b8aa050 · Sep 27, 2023

History

History
This branch is 5 commits behind DesignPatternsPHP/DesignPatternsPHP:main.

SimpleFactory

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Oct 1, 2021
Aug 19, 2019
Oct 1, 2021
Sep 27, 2023
Oct 1, 2021

README.rst

Simple Factory

Purpose

SimpleFactory is a simple factory pattern.

It differs from the static factory because it is not static. Therefore, you can have multiple factories, differently parameterized, you can subclass it and you can mock it. It always should be preferred over a static factory!

UML Diagram

Alt SimpleFactory UML Diagram

Code

You can also find this code on GitHub

SimpleFactory.php

.. literalinclude:: SimpleFactory.php
   :language: php
   :linenos:

Bicycle.php

.. literalinclude:: Bicycle.php
   :language: php
   :linenos:

Usage

 $factory = new SimpleFactory();
 $bicycle = $factory->createBicycle();
 $bicycle->driveTo('Paris');

Test

Tests/SimpleFactoryTest.php

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