1.
Singleton Pattern
Singleton Pattern – The One and Only Wizard
In the village, there’s a wizard who knows all the spells,
but the villagers decided they only need one wizard to save resources.
They built a special house where only one wizard instance can live.
No matter how many times someone calls the wizard, the same wizard comes
out.
In PHP, this is like creating a class where only one instance of it can exist.
class Wizard {
private static $instance = null; // The one and only instance
private function __construct() {
echo "A wizard is created!";
}
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new Wizard();
}
return self::$instance;
}
}
$wizard1 = Wizard::getInstance();
$wizard2 = Wizard::getInstance();
var_dump($wizard1 === $wizard2); // true, same wizard
2. Factory Pattern
Factory Pattern – The Potion Factory
In the village, there’s a factory that creates potions for the villagers. If someone
asks for a healing potion, it gives them one. If someone asks for a strength
potion, it creates that too. The villagers don’t need to know how the potions are
made—the factory takes care of it.
In PHP, the Factory Pattern is about a class that creates objects without the user
worrying about the creation logic.
class PotionFactory {
public static function createPotion($type) {
if ($type === 'healing') {
return new HealingPotion();
} elseif ($type === 'strength') {
return new StrengthPotion();
}
return null;
}
}
class HealingPotion {
public function __construct() {
echo "Healing Potion created!";
}
}
class StrengthPotion {
public function __construct() {
echo "Strength Potion created!";
}
}
// Villager asks for potions
$healingPotion = PotionFactory::createPotion('healing');
$strengthPotion = PotionFactory::createPotion('strength');
3. Observer Pattern
Observer Pattern – The Village Alarm Bell
The village has an alarm bell. If something dangerous happens, the bell rings,
and all villagers are notified. Villagers can choose to subscribe to the alarm
bell or ignore it.
In PHP, the Observer Pattern allows objects (villagers) to listen for updates
from another object (alarm bell).
class AlarmBell {
private $observers = []; // List of villagers
public function addObserver($observer) {
$this->observers[] = $observer;
}
public function ringBell() {
foreach ($this->observers as $observer) {
$observer->notify();
}
}
}
class Villager {
private $name;
public function __construct($name) {
$this->name = $name;
}
public function notify() {
echo "{$this->name} heard the alarm bell!\n";
}
}
// Create the alarm bell and villagers
$alarm = new AlarmBell();
$john = new Villager("John");
$anna = new Villager("Anna");
// Villagers subscribe to the alarm
$alarm->addObserver($john);
$alarm->addObserver($anna);
// Ring the alarm bell
$alarm->ringBell();
4. Strategy Pattern
Strategy Pattern – The Knight’s Battle Plans
The village’s knight uses different battle strategies depending on the enemy.
Against dragons, he uses a fireproof shield. Against trolls, he uses a heavy
hammer. He can switch strategies anytime.
In PHP, the Strategy Pattern is about changing an object’s behavior
dynamically based on the situation.
interface BattleStrategy {
public function fight();
}
class DragonStrategy implements BattleStrategy {
public function fight() {
echo "Using fireproof shield against the dragon!";
}
}
class TrollStrategy implements BattleStrategy {
public function fight() {
echo "Using heavy hammer against the troll!";
}
}
class Knight {
private $strategy;
public function setStrategy(BattleStrategy $strategy) {
$this->strategy = $strategy;
}
public function battle() {
$this->strategy->fight();
}
}
// The knight fights enemies with different strategies
$knight = new Knight();
$knight->setStrategy(new DragonStrategy());
$knight->battle();
$knight->setStrategy(new TrollStrategy());
$knight->battle();
5. Decorator Pattern
Decorator Pattern – The Village Tailor
The village has a tailor who makes basic clothes. But if you want extra features
like armor, magic runes, or fancy stitching, the tailor adds layers to the basic
clothing without starting from scratch.
In PHP, the Decorator Pattern is about adding functionality to an object
dynamically.
interface Clothing {
public function wear();
}
class BasicClothing implements Clothing {
public function wear() {
echo "Wearing basic clothing.";
}
}
class ArmorDecorator implements Clothing {
private $clothing;
public function __construct(Clothing $clothing) {
$this->clothing = $clothing;
}
public function wear() {
$this->clothing->wear();
echo " Adding armor.";
}
}
class MagicRunesDecorator implements Clothing {
private $clothing;
public function __construct(Clothing $clothing) {
$this->clothing = $clothing;
}
public function wear() {
$this->clothing->wear();
echo " Adding magic runes.";
}
}
// Create clothing and add features
$clothing = new BasicClothing();
$clothingWithArmor = new ArmorDecorator($clothing);
$clothingWithArmorAndRunes = new
MagicRunesDecorator($clothingWithArmor);
$clothingWithArmorAndRunes->wear();