Skip to content

Latest commit

 

History

History

FactoryMethod

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

Logger.php

StdoutLogger.php

StdoutLogger.php

FileLogger.php

FileLogger.php

LoggerFactory.php

LoggerFactory.php

StdoutLoggerFactory.php

StdoutLoggerFactory.php

FileLoggerFactory.php

FileLoggerFactory.php

Test

Tests/FactoryMethodTest.php

Tests/FactoryMethodTest.php