Wednesday, August 02, 2023

Prototype Design patter A creationlal pattern as taught by TheRayCode

We start with the Phrase class This class is part of the main class we desire to clone.. The Phrase class is straightforward. It's a simple class with a phrase type property.
Phrase{

    public $phrase;

    public function __construct($phrase) {
        $this->phrase = $phrase;
    }
}
The Clown class has two properties: name (a string) and nose (an instance of the Nose class). We also have a magic method __clone() for shallow cloning and a deepClone() method for deep cloning.

When you clone an object in PHP, by default it does a shallow copy. A shallow copy means creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type, a bit-by-bit copy of the field is performed; If a field is a reference type, the reference is copied but the referred object is not; hence the original and its clone refer to the same object.

class Clown
{
    public $name;
    public $chatchPrase;

    public function __construct($name, Phrase $prase) {
        $this->name = $name;
        $this->chatchPrase = $prase;
    }

    // Shallow clone
    public function __clone() {
        // When you clone the Clown object, PHP does a shallow copy by default.
    }

    // Deep clone
    public function deepClone() {
        $cloned = clone $this;
        $cloned->chatchPrase = clone $this->chatchPrase;  // clone inner object
        return $cloned;
    }
}
In the index.php file, we first create an instance of the Clown class named bozoClown. Then we create a shallow clone (bozoShallowClone) and a deep clone (bozoDeepClone) of this object.

We then change the color of the nose property on the original bozoClown. After that, we print the color of the nose property for the original, the shallow clone, and the deep clone.

Because the shallow clone only copied the reference to the Prase object, when we change the color of the nose in the original bozoClown, it is also changed in the bozoShallowClone (they both point to the same Nose object).

However, in the deep clone, we manually cloned the Nose object as well, so bozoDeepClone has its own Nose object, and changes to the original bozoClown's nose do not affect it.
In the index.php we have:
first our include fines
include 'Clown.php';
include 'Phrase.php';

$bozoClown = new Clown("Bozo", new Phrase("Merry Christmas"));
and then we declare bozoClown
$bozoShallowClone = clone $bozoClown;
$KrampusKlown = $bozoClown->deepClone();

// Let's change the Prase of ORIGINAL bozoClown
$bozoClown->chatchPrase->phrase = "WHOA NELLY";

echo "Original Bozo Clown chatch Prase: " . $bozoClown->chatchPrase->phrase . "
\n"; // WHOA NELLY
After that, we print the color of the nose property for the original, the shallow clone, and the deep clone.
echo "Shallow Bozo Clone phrase: " . $bozoShallowClone->chatchPrase->phrase . "
\n"; // WHOA NELLY echo "Deep Krampus Clone phase: " . $KrampusKlown->chatchPrase->phrase . "
\n"; // Merry Christmas
Because the shallow clone only copied the reference to the Nose object, when we change the the phrase in the original bozoClown, it is also changed in the bozoShallowClone (they both point to the same Phrase object).


However, in the deep clone, we manually cloned the Phrase object as well, so bozoDeepClone has its own Phrase object, and changes to the original bozoClown's phrase do not affect it.

When we view it in the proser we get
Original Bozo Clown chatch Prase: WHOA NELLY
Shallow Bozo Clone phrase: WHOA NELLY
Deep Krampus Clone phase: Merry Christmas
TheRayCode is AWESOME!!!

The Strategy Design Pattern a Behavioral Pattern using C++

The Strategy Design Pattern is a behavioral design pattern that enables selecting an algorithm's implementation at runtime. Instead of i...