IT creational patterns: Difference between revisions

From A Technological Wiki
Jump to navigation Jump to search
imported>Unknown user
No edit summary
No edit summary
 
(2 intermediate revisions by one other user not shown)
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.
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>
 
<span id="when-to-use-them"></span>
== When to use them ==
== 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 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 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.
* 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>
 
<span id="fundamental-creational-patterns"></span>
= Fundamental creational patterns =
= Fundamental creational patterns =
<span id="singleton"></span>
<span id="singleton"></span>
== Singleton ==
== Singleton ==
Line 16: Line 11:
Ensure a class has only one instance and provide a global point to access it.
Ensure a class has only one instance and provide a global point to access it.


<syntaxhighlight>class Singleton {
<syntaxhighlight lang="php">class Singleton {
     private static $instance = null;
     private static $instance = null;
     private function __construct() {}
     private function __construct() {}
Line 27: Line 22:
     }
     }
}
}
$instance = Singleton::getInstance();</syntaxhighlight>
$instance = Singleton::getInstance();</syntaxhighlight><span id="factory-method"></span>
<span id="factory-method"></span>
== Factory Method ==
== 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.
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 {
<syntaxhighlight lang="php">interface Product {
     public function create();
     public function create();
}
}
Line 39: Line 33:
class ConcreteProductA implements Product {
class ConcreteProductA implements Product {
     public function create() {
     public function create() {
         return &quot;Product A&quot;;
         return "Product A";
     }
     }
}
}
Line 45: Line 39:
class ConcreteProductB implements Product {
class ConcreteProductB implements Product {
     public function create() {
     public function create() {
         return &quot;Product B&quot;;
         return "Product B";
     }
     }
}
}
Line 66: Line 60:


$creator = new ConcreteCreatorA();
$creator = new ConcreteCreatorA();
$product = $creator-&gt;factoryMethod();
$product = $creator->factoryMethod();
echo $product-&gt;create();  // Outputs &quot;Product A&quot;</syntaxhighlight>
echo $product->create();  // Outputs "Product A"</syntaxhighlight>
<span id="abstract-factory"></span>
<span id="abstract-factory"></span>
== Abstract Factory ==
== Abstract Factory ==
Line 73: Line 67:
Creates an object from several families of classes without specifying the exact class name.
Creates an object from several families of classes without specifying the exact class name.


<syntaxhighlight>interface Button {
<syntaxhighlight lang="php">interface Button {
     public function paint();
     public function paint();
}
}
Line 79: Line 73:
class WinButton implements Button {
class WinButton implements Button {
     public function paint() {
     public function paint() {
         return &quot;Windows Button&quot;;
         return "Windows Button";
     }
     }
}
}
Line 85: Line 79:
class MacButton implements Button {
class MacButton implements Button {
     public function paint() {
     public function paint() {
         return &quot;Mac Button&quot;;
         return "Mac Button";
     }
     }
}
}
Line 106: Line 100:


$guiFactory = new MacFactory();
$guiFactory = new MacFactory();
$button = $guiFactory-&gt;createButton();
$button = $guiFactory->createButton();
echo $button-&gt;paint();  // Outputs &quot;Mac Button&quot;</syntaxhighlight>
echo $button->paint();  // Outputs "Mac Button"</syntaxhighlight>
<span id="prototype"></span>
<span id="prototype"></span>
== Prototype ==
== Prototype ==
Line 113: Line 107:
Creates objects by cloning an existing object, known as the prototype.
Creates objects by cloning an existing object, known as the prototype.


<syntaxhighlight>abstract class Prototype {
<syntaxhighlight lang="php">abstract class Prototype {
     abstract public function __clone();
     abstract public function __clone();
}
}
Line 123: Line 117:


$prototype = new ConcretePrototype();
$prototype = new ConcretePrototype();
$prototype-&gt;property = &quot;Original&quot;;
$prototype->property = "Original";
$clone = clone $prototype;
$clone = clone $prototype;
$clone-&gt;property = &quot;Cloned&quot;;
$clone->property = "Cloned";
echo $prototype-&gt;property;  // Outputs &quot;Original&quot;
echo $prototype->property;  // Outputs "Original"
echo $clone-&gt;property;      // Outputs &quot;Cloned&quot;</syntaxhighlight>
echo $clone->property;      // Outputs "Cloned"</syntaxhighlight><span id="builder"></span>
<span id="builder"></span>
== Builder ==
== Builder ==


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


<syntaxhighlight>class Product {
<syntaxhighlight lang="php">class Product {
     private $parts = [];
     private $parts = [];


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


     public function show() {
     public function show() {
         return implode(&quot;, &quot;, $this-&gt;parts);
         return implode(", ", $this->parts);
     }
     }
}
}
Line 149: Line 142:


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


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


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


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


$builder = new Builder();
$builder = new Builder();
$builder-&gt;buildPartA();
$builder->buildPartA();
$builder-&gt;buildPartB();
$builder->buildPartB();
$product = $builder-&gt;getResult();
$product = $builder->getResult();
echo $product-&gt;show();  // Outputs &quot;PartA, PartB&quot;</syntaxhighlight>
echo $product->show();  // Outputs "PartA, PartB"</syntaxhighlight>
<!-- From bodyContent 1542977.mraw -->
[[Category:IT backend patterns]]
 

Latest revision as of 22:01, 20 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 "Product A";
    }
}

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

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->factoryMethod();
echo $product->create();  // Outputs "Product A"

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 "Windows Button";
    }
}

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

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->createButton();
echo $button->paint();  // Outputs "Mac Button"

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->property = "Original";
$clone = clone $prototype;
$clone->property = "Cloned";
echo $prototype->property;  // Outputs "Original"
echo $clone->property;      // Outputs "Cloned"

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->parts[] = $part;
    }

    public function show() {
        return implode(", ", $this->parts);
    }
}

class Builder {
    private $product;

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

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

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

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

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