IT creational patterns: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
Added category IT backend patterns
Line 172: Line 172:
  <!-- From bodyContent 1542977.mraw -->
  <!-- From bodyContent 1542977.mraw -->


[[Category:IT backend patterns]]

Revision as of 18:51, 19 October 2025

Creational patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

When to use them

  • When the creation process is complex: If creating an object is a multi-step process or requires logic, it's best encapsulated.
  • When you need to decouple the system from specific classes: This helps in making the system more modular and flexible.
  • When you want to abstract the instantiation process: Hide the logic of creating objects and just present a ready-to-use object.

Fundamental creational patterns

Singleton

Ensure a class has only one instance and provide a global point to access it.

class Singleton {
    private static $instance = null;
    private function __construct() {}

    public static function getInstance() {
        if (self::$instance == null) {
            self::$instance = new Singleton();
        }
        return self::$instance;
    }
}
$instance = Singleton::getInstance();

Factory Method

A method for creating an object in a super class but allows subclasses to alter the type of objects that will be created.

interface Product {
    public function create();
}

class ConcreteProductA implements Product {
    public function create() {
        return &quot;Product A&quot;;
    }
}

class ConcreteProductB implements Product {
    public function create() {
        return &quot;Product B&quot;;
    }
}

abstract class Creator {
    abstract public function factoryMethod(): Product;
}

class ConcreteCreatorA extends Creator {
    public function factoryMethod(): Product {
        return new ConcreteProductA();
    }
}

class ConcreteCreatorB extends Creator {
    public function factoryMethod(): Product {
        return new ConcreteProductB();
    }
}

$creator = new ConcreteCreatorA();
$product = $creator-&gt;factoryMethod();
echo $product-&gt;create();  // Outputs &quot;Product A&quot;

Abstract Factory

Creates an object from several families of classes without specifying the exact class name.

interface Button {
    public function paint();
}

class WinButton implements Button {
    public function paint() {
        return &quot;Windows Button&quot;;
    }
}

class MacButton implements Button {
    public function paint() {
        return &quot;Mac Button&quot;;
    }
}

interface GUIFactory {
    public function createButton(): Button;
}

class WinFactory implements GUIFactory {
    public function createButton(): Button {
        return new WinButton();
    }
}

class MacFactory implements GUIFactory {
    public function createButton(): Button {
        return new MacButton();
    }
}

$guiFactory = new MacFactory();
$button = $guiFactory-&gt;createButton();
echo $button-&gt;paint();  // Outputs &quot;Mac Button&quot;

Prototype

Creates objects by cloning an existing object, known as the prototype.

abstract class Prototype {
    abstract public function __clone();
}

class ConcretePrototype extends Prototype {
    public $property;
    public function __clone() {}
}

$prototype = new ConcretePrototype();
$prototype-&gt;property = &quot;Original&quot;;
$clone = clone $prototype;
$clone-&gt;property = &quot;Cloned&quot;;
echo $prototype-&gt;property;  // Outputs &quot;Original&quot;
echo $clone-&gt;property;      // Outputs &quot;Cloned&quot;

Builder

Allows constructing complex objects step by step. Separates the construction of a complex object from its representation.

class Product {
    private $parts = [];

    public function add($part) {
        $this-&gt;parts[] = $part;
    }

    public function show() {
        return implode(&quot;, &quot;, $this-&gt;parts);
    }
}

class Builder {
    private $product;

    public function __construct() {
        $this-&gt;product = new Product();
    }

    public function buildPartA() {
        $this-&gt;product-&gt;add('PartA');
    }

    public function buildPartB() {
        $this-&gt;product-&gt;add('PartB');
    }

    public function getResult() {
        return $this-&gt;product;
    }
}

$builder = new Builder();
$builder-&gt;buildPartA();
$builder-&gt;buildPartB();
$product = $builder-&gt;getResult();
echo $product-&gt;show();  // Outputs &quot;PartA, PartB&quot;