IT creational patterns: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
imported>Unknown user
No edit summary
imported>Unknown user
No edit summary
Line 1: Line 1:
Creational patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.


<span id="when-to-use-them"></span>
== 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.
<span id="fundamental-creational-patterns"></span>
= Fundamental creational patterns =
<span id="singleton"></span>
== Singleton ==
Ensure a class has only one instance and provide a global point to access it.
<syntaxhighlight>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();</syntaxhighlight>
<span id="factory-method"></span>
== 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.
<syntaxhighlight>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;</syntaxhighlight>
<span id="abstract-factory"></span>
== Abstract Factory ==
Creates an object from several families of classes without specifying the exact class name.
<syntaxhighlight>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;</syntaxhighlight>
<span id="prototype"></span>
== Prototype ==
Creates objects by cloning an existing object, known as the prototype.
<syntaxhighlight>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;</syntaxhighlight>
<span id="builder"></span>
== Builder ==
Allows constructing complex objects step by step. Separates the construction of a complex object from its representation.
<syntaxhighlight>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;</syntaxhighlight>
  <!-- From bodyContent 1542977.mraw -->
  <!-- From bodyContent 1542977.mraw -->



Revision as of 11:10, 19 April 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;