Sobes.tech
Middle

PHP-də dekorator necə işləyir?

sobes.tech Süni İntellekt

AI-dan cavab

"Dekorator" nümunəsi mövcud obyektə dinamik olaraq yeni davranış əlavə edir və onu başqa obyekt, yəni dekorator, ilə örtür.

  • Tərkib hissələri:

    • İnterfeys (Component): Orijinal obyekt və onun dekoratorları üçün ümumi metodları elan edir.
    • Əsas Komponent (Concrete Component): Dekorasiya ediləcək əsas obyekt.
    • Əsas Dekorator (Decorator): İnterfeys (Component) obyektinə istinad edən və onun metodlarını həyata keçirən abstrakt sinf və ya interfeys, adətən bu obyektə çağırışı yönəldir.
    • Əsas Decoratorlar (Concrete Decorators): Yeni imkanlar tətbiq edir, əsas komponentin davranışını tamamlayır və ya dəyişdirir.
  • İş prinsipi:

    • Dekorator içərisində dekorasiya etdiyi obyektə istinad edir (birləşmə vasitəsilə).
    • Eyni interfeysə malikdir, yəni dekorasiya olunan obyektlə eyni interfeysə sahibdir.
    • Dekorator üzərində metod çağırıldıqda, o, əlavə məntiqi əvvəl və ya sonra icra edir və ya tamamilə onun yerinə keçir (sonuncu, bu, tipik istifadə deyil).
  • Üstünlüklər:

    • Ob'ektlərə iş vaxtında funksionallıq əlavə etməyə imkan verir, statik irs ilə deyil.
    • Müxtəlif davranışların birləşməsində sinif hierarxiyasının partlamasını qarşısını alır.
    • Esnek davranış kombinasiyalarını yaratmağa imkan verir.
  • Nümunə:

<?php

// Komponent interfeysi
interface Coffee {
    public function getCost(): float;
    public function getDescription(): string;
}

// Konkret komponent
class SimpleCoffee implements Coffee {
    public function getCost(): float {
        return 5.0;
    }

    public function getDescription(): string {
        return "Sadə qəhvə";
    }
}

// Əsas dekorator
abstract class CoffeeDecorator implements Coffee {
    protected Coffee $coffee;

    public function __construct(Coffee $coffee) {
        $this->coffee = $coffee;
    }

    public function getCost(): float {
        return $this->coffee->getCost();
    }

    public function getDescription(): string {
        return $this->coffee->getDescription();
    }
}

// Konkret dekorator: Süd əlavə etmək
class MilkDecorator extends CoffeeDecorator {
    public function getCost(): float {
        return parent::getCost() + 2.0;
    }

    public function getDescription(): string {
        return parent::getDescription() . ", süd ilə";
    }
}

// Konkret dekorator: Şəkər əlavə etmək
class SugarDecorator extends CoffeeDecorator {
    public function getCost(): float {
        return parent::getCost() + 1.0;
    }

    public function getDescription(): string {
        return parent::getDescription() . ", şəkər ilə";
    }
}

// İstifadə:
// Sadə qəhvə yaradılır
$coffee = new SimpleCoffee();
echo $coffee->getDescription() . " - " . $coffee->getCost() . "\n"; // Sadə qəhvə - 5

// Süd əlavə edilir
$coffeeWithMilk = new MilkDecorator($coffee);
echo $coffeeWithMilk->getDescription() . " - " . $coffeeWithMilk->getCost() . "\n"; // Sadə qəhvə, süd ilə - 7

// Şəkər əlavə edilir
$coffeeWithMilkAndSugar = new SugarDecorator($coffeeWithMilk);
echo $coffeeWithMilkAndSugar->getDescription() . " - " . $coffeeWithMilkAndSugar->getCost() . "\n"; // Sadə qəhvə, süd ilə, şəkər ilə - 8