Creational Design Patterns are a set of design patterns that deal with object creation mechanisms. They provide solutions to common software design problems by creating objects in a way that is efficient, scalable, and maintainable. By following established design patterns, developers can ensure their code is reusable and efficient, resulting in faster development and easier collaboration.
Table of Contents
Types of Creational Design Patterns in PHP
There are five main types of Creational Design Patterns in PHP, including
- Singleton Pattern.
- Factory Pattern.
- Abstract Factory Pattern.
- Builder Pattern.
- Prototype Pattern.
Singleton Pattern
The Singleton Pattern ensures that only one instance of a class exists in the entire application. It is useful in situations where having multiple instances of a class can cause issues, such as with database connections.
class Singleton {
private static $instance;
private function __construct() {}
public static function getInstance() {
if(!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
}
Factory Pattern
interface Shape {
public function draw();
}
class Circle implements Shape {
public function draw() {
echo "Drawing Circle";
}
}
class Square implements Shape {
public function draw() {
echo "Drawing Square";
}
}
class ShapeFactory {
public static function createShape($type) {
switch($type) {
case 'circle':
return new Circle();
case 'square':
return new Square();
default:
throw new Exception("Invalid shape type.");
}
}
}
$circle = ShapeFactory::createShape('circle');
$circle->draw(); // Outputs "Drawing Circle"
Abstract Factory Pattern
The Abstract Factory Pattern is used to create families of related or dependent objects. It allows for the creation of multiple related objects without specifying their classes.
interface Shape {
public function draw();
}
class Circle implements Shape {
public function draw() {
echo "Drawing Circle";
}
}
Builder Pattern
The Builder Pattern is a Creational Design Pattern that separates the construction of complex objects from their representation, allowing for the creation of different representations of an object using the same construction process. This pattern is useful when constructing complex objects that require a lot of configuration.
class HouseBuilder {
private $wallMaterial;
private $roofMaterial;
private $windowStyle;
public function setWallMaterial($material) {
$this->wallMaterial = $material;
}
public function setRoofMaterial($material) {
$this->roofMaterial = $material;
}
public function setWindowStyle($style) {
$this->windowStyle = $style;
}
public function build() {
return new House($this->wallMaterial, $this->roofMaterial, $this->windowStyle);
}
}
class House {
private $wallMaterial;
private $roofMaterial;
private $windowStyle;
public function __construct($wallMaterial, $roofMaterial, $windowStyle) {
$this->wallMaterial = $wallMaterial;
$this->roofMaterial = $roofMaterial;
$this->windowStyle = $windowStyle;
}
public function getWallMaterial() {
return $this->wallMaterial;
}
public function getRoofMaterial() {
return $this->roofMaterial;
}
public function getWindowStyle() {
return $this->windowStyle;
}
}
// Usage example
$builder = new HouseBuilder();
$builder->setWallMaterial('Brick')
->setRoofMaterial('Tile')
->setWindowStyle('Bay Window');
$house = $builder->build();
echo $house->getWallMaterial(); // Output: Brick
echo $house->getRoofMaterial(); // Output: Tile
echo $house->getWindowStyle(); // Output: Bay Window
Prototype Pattern
The Prototype Pattern is a Creational Design Pattern that allows developers to create new objects by cloning existing objects. This pattern is useful when creating objects that require a lot of configuration and can take a long time to create.
abstract class Shape {
protected $type;
public function __construct($type) {
$this->type = $type;
}
public abstract function draw();
}
class Rectangle extends Shape {
public function __construct($type) {
parent::__construct($type);
}
public function draw() {
echo "Drawing a rectangle";
}
}
class Circle extends Shape {
public function __construct($type) {
parent::__construct($type);
}
public function draw() {
echo "Drawing a circle";
}
}
class ShapeCache {
private static $shapeMap = array();
public static function getShape($shapeId) {
$cachedShape = self::$shapeMap[$shapeId];
return clone $cachedShape;
}
public static function loadCache() {
$circle = new Circle('1');
self::$shapeMap[$circle->type] = $circle;
$rectangle = new Rectangle('2');
self::$shapeMap[$rectangle->type] = $rectangle;
}
}
// Usage example
ShapeCache::loadCache();
$cloneShape1 = ShapeCache::getShape('1');
echo $cloneShape1->draw(); // Output: Drawing a circle
$cloneShape2 = ShapeCache::getShape('2');
echo $cloneShape2->draw(); // Output: Drawing a rectangle
Conclusion
Creational Design Patterns are essential for creating efficient, scalable, and maintainable code in PHP. The five types of Creational Design Patterns in PHP, including the Singleton Pattern, Factory Pattern, Abstract Factory Pattern, Builder Pattern, and Prototype Pattern, provide various ways of creating objects that can improve code quality.