
Creational patterns
Creational patterns, as the name suggests, create objects for us, so we do not have to instantiate them directly. Implementing creation patterns gives our application a level of flexibility, where the application itself can decide what objects to instantiate at a given time. The following is a list of patterns we categorize as creational patterns:
- Abstract factory pattern
- Builder pattern
- Factory method pattern
- Prototype pattern
- Singleton pattern
Note
See creational design patterns.
Abstract factory pattern
Building portable applications requires a great level of dependencies encapsulation. The abstract factory facilitates this by abstracting the creation of families of related or dependent objects. Clients never create these platform objects directly, the factory does it for them, making it possible to interchange concrete implementations without changing the code that uses them, even at runtime.
The following is an example of possible abstract factory pattern implementation:
interface Button { public function render(); } interface GUIFactory { public function createButton(); } class SubmitButton implements Button { public function render() { echo 'Render Submit Button'; } } class ResetButton implements Button { public function render() { echo 'Render Reset Button'; } } class SubmitFactory implements GUIFactory { public function createButton() { return new SubmitButton(); } } class ResetFactory implements GUIFactory { public function createButton() { return new ResetButton(); } } // Client $submitFactory = new SubmitFactory(); $button = $submitFactory->createButton(); $button->render(); $resetFactory = new ResetFactory(); $button = $resetFactory->createButton(); $button->render();
We started off by creating an interface Button
, which is later implemented by our SubmitButton
and ResetButton
concrete classes. GUIFactory
and ResetFactory
implement the GUIFactory
interface, which specifies the createButton
method. The client then simply instantiates factories and calls for createButton
, which returns a proper button instance that we call the render
method.
Builder pattern
The builder pattern separates the construction of a complex object from its representation, making it possible for the same construction process to create different representations. While some creational patterns construct a product in one call, builder pattern does it step by step under the control of the director.
The following is an example of builder pattern implementation:
class Car { public function getWheels() { /* implementation... */ } public function setWheels($wheels) { /* implementation... */ } public function getColour($colour) { /* implementation... */ } public function setColour() { /* implementation... */ } } interface CarBuilderInterface { public function setColour($colour); public function setWheels($wheels); public function getResult(); } class CarBuilder implements CarBuilderInterface { private $car; public function __construct() { $this->car = new Car(); } public function setColour($colour) { $this->car->setColour($colour); return $this; } public function setWheels($wheels) { $this->car->setWheels($wheels); return $this; } public function getResult() { return $this->car; } } class CarBuildDirector { private $builder; public function __construct(CarBuilder $builder) { $this->builder = $builder; } public function build() { $this->builder->setColour('Red'); $this->builder->setWheels(4); return $this; } public function getCar() { return $this->builder->getResult(); } } // Client $carBuilder = new CarBuilder(); $carBuildDirector = new CarBuildDirector($carBuilder); $car = $carBuildDirector->build()->getCar();
We started off by creating a concrete Car
class with several methods defining some base characteristics of a car. We then created a CarBuilderInterface
that will control some of those characteristics and get the final result (car
). The concrete class CarBuilder
then implemented the CarBuilderInterface
, followed by the concrete CarBuildDirector
class, which defined build and the getCar
method. The client then simply instantiated a new instance of CarBuilder
, passing it as a constructor parameter to a new instance of CarBuildDirector
. Finally, we called the build
and getCar
methods of CarBuildDirector
to get the actual car Car
instance.
Factory method pattern
The factory
method pattern deals with the problem of creating objects without having to specify the exact class of the object that will be created.
The following is an example of factory method pattern implementation:
interface Product { public function getType(); } interface ProductFactory { public function makeProduct(); } class SimpleProduct implements Product { public function getType() { return 'SimpleProduct'; } } class SimpleProductFactory implements ProductFactory { public function makeProduct() { return new SimpleProduct(); } } /* Client */ $factory = new SimpleProductFactory(); $product = $factory->makeProduct(); echo $product->getType(); //outputs: SimpleProduct
We started off by creating a ProductFactory
and Product
interfaces. The SimpleProductFactory
implements the ProductFactory
and returns the new product
instance via its makeProduct
method. The SimpleProduct
class implements Product
, and returns the product type. Finally, the client creates the instance of SimpleProductFactory
, calling the makeProduct
method on it. The makeProduct
returns the instance of the Product
, whose getType
method returns the SimpleProduct
string.
Prototype pattern
The prototype pattern replicates other objects by use of cloning. What this means is that we are not using the new
keyword to instantiate new objects. PHP provides a clone
keyword which makes a shallow copy of an object, thus providing pretty much straight forward prototype pattern implementation. Shallow copy does not copy references, only values to the new object. We can further utilize the magic __clone
method on our class in order to implement more robust clone behavior.
The following is an example of prototype pattern implementation:
class User { public $name; public $email; } class Employee extends User { public function __construct() { $this->name = 'Johhn Doe'; $this->email = 'john.doe@fake.mail'; } public function info() { return sprintf('%s, %s', $this->name, $this->email); } public function __clone() { /* additional changes for (after)clone behavior? */ } } $employee = new Employee(); echo $employee->info(); $director = clone $employee; $director->name = 'Jane Doe'; $director->email = 'jane.doe@fake.mail'; echo $director->info(); //outputs: Jane Doe, jane.doe@fake.mail
We started off by creating a simple User
class. The Employee
then extends the User
, while setting name
and email
in its constructor. The client then instantiates the Employee
via the new
keyword, and clones it into the director
variable. The $director
variable is now a new instance, one made not by the new
keyword, but with cloning, using the clone
keyword. Changing name
and email
on $director
, does not affect $employee
.
Singleton pattern
The purpose of singleton pattern is to restrict instantiation of class to a single object. It is implemented by creating a method within the class that creates a new instance of that class if one does not exist. If an object instance already exists, the method simply returns a reference to an existing object.
The following is an example of singleton pattern implementation:
class Logger { private static $instance; public static function getInstance() { if (!isset(self::$instance)) { self::$instance = new self; } return self::$instance; } public function logNotice($msg) { return 'logNotice: ' . $msg; } public function logWarning($msg) { return 'logWarning: ' . $msg; } public function logError($msg) { return 'logError: ' . $msg; } } // Client echo Logger::getInstance()->logNotice('test-notice'); echo Logger::getInstance()->logWarning('test-warning'); echo Logger::getInstance()->logError('test-error'); // Outputs: // logNotice: test-notice // logWarning: test-warning // logError: test-error
We started off by creating a Logger
class with a static $instance
member, and the getInstance
method that always returns a single instance of the class. Then we added a few sample methods to demonstrate the client executing various methods on a single instance.