vendredi 1 septembre 2023

php delegation pattern example

I am studing Kotlin and understand better the Delegation Pattern I've just asked to ChatGPT an example of Delegation Pattern for PHP but give me only a lambda example

function eseguiOperazione($operazione, $valore) {
    return $operazione($valore);
}

$raddoppia = function ($numero) {
    return $numero * 2;
};

$risultato = eseguiOperazione($raddoppia, 5);

echo $risultato;

I just bake this, is feasible like "example" for delegation pattern for php? If not, Why?

<?php
interface MyObject {
    public function Action();
}

class Concrete implements MyObject {
    public function Action() {
        return 'actionConcrete';
    }
}

class Concrete2 implements MyObject {
    public function Action() {
        return 'actionConcrete2';
    }
}

class Delegant {
    public MyObject $O;
    public function __construct(MyObject $obj) {
        $this->O = $obj;
    }
    public function Action() {
        $this->O->Action();
    }
}

$c = new Delegant(new Concrete2());
echo $c->Action();

Aucun commentaire:

Enregistrer un commentaire