Affichage des articles dont le libellé est design patterns. Afficher tous les articles
Affichage des articles dont le libellé est design patterns. Afficher tous les articles

vendredi 13 avril 2012

Design pattern strategy

Définition

Le design pattern strategy définit une famille d'algorithmes, encapsule chacun d'eux, et les rend interchangeables. Il laisse l’algorithme changer indépendamment des clients qui les utilisent.

Diagramme UML

Diagramme de classes du design pattern strategy

Exemple en PHP

// The classes that implement a concrete strategy should implement this
// The context class uses this to call the concrete strategy
interface Strategy
{
    public function execute($a, $b);
}

// Implements the algorithm using the strategy interface
class ConcreteStrategyAdd implements Strategy
{
    public function execute($a, $b)
    {
       echo 'Called ConcreteStrategyAdd\'s execute()' . "\n";
       return $a + $b;  // Do an addition with a and b
    }
}

class ConcreteStrategySubtract implements Strategy
{
    public function execute($a, $b)
    {
       echo 'Called ConcreteStrategySubtract\'s execute()' . "\n";
       return $a - $b;  // Do a subtraction with a and b
    }
}

class ConcreteStrategyMultiply implements Strategy
{
    public function execute($a, $b)
    {
       echo 'Called ConcreteStrategyMultiply\'s execute()' . "\n";
       return $a * $b;   // Do a multiplication with a and b
    }    
}

// Configured with a ConcreteStrategy object and maintains a reference 
// to a Strategy object
class Context
{
    private $strategy;

    public function __construct(Strategy $strategy)
    {
        $this->setStrategy($strategy);
    }

    public function executeStrategy($a, $b)
    {
        return $this->strategy->execute($a, $b);
    }

    public function setStrategy(Strategy $strategy)
    {
        $this->strategy = $strategy;
    }
}

// Three contexts following different strategies
$context = new Context(new ConcreteStrategyAdd());
echo $context->executeStrategy(3, 4) . "\n";

$context->setStrategy(new ConcreteStrategySubtract());
echo $context->executeStrategy(3, 4) . "\n";

$context->setStrategy(new ConcreteStrategyMultiply());
echo $context->executeStrategy(3, 4) . "\n";
L'exemple affiche :
Called ConcreteStrategyAdd's execute()
7
Called ConcreteStrategySubtract's execute()
-1
Called ConcreteStrategyMultiply's execute()
12

vendredi 22 avril 2011

Design pattern observer

Définition

Le design pattern observer définit une relation un-à-plusieurs entre des objets de sorte que quand un objet change d'état, toutes ses relations sont notifiées et mises à jour automatiquement.

Diagramme UML

Diagramme de classes du design pattern observer


Exemple en PHP

Depuis PHP 5.1, la Standard PHP Library (SPL) fournit les interfaces SplSubject et SplObserver pour implémenter le design pattern observer.
class Subject implements SplSubject
{
    private $observers = array();
    private $variable;
    
    public function attach(SplObserver $observer)
    {
        $id = spl_object_hash($observer);
        $this->observers[$id] = $observer;
    }
    
    public function detach(SplObserver $observer)
    {
        $id = spl_object_hash($observer);
        unset($this->observers[$id]);
    }
    
    public function getVariable()
    {
        return $this->variable;
    }
    
    public function notify()
    {
        foreach ($this->observers as $observer) {
            $observer->update($this);
        }
    }
    
    public function setVariable($value)
    {
        $this->variable = $value;
        $this->notify();
    }
}

class ConcreteObserverA implements SplObserver
{
    public function update(SplSubject $subject)
    {
        echo 'ConcreteObserverA received: ' . $subject->getVariable() . "\n";
    }
}

class ConcreteObserverB implements SplObserver
{
    public function update(SplSubject $subject)
    {
        echo 'ConcreteObserverB received: ' . $subject->getVariable() . "\n";
    }
}

$subject = new Subject();
$subject->attach(new ConcreteObserverA());
$subject->attach(new ConcreteObserverB());
$subject->setVariable(42);
L'exemple affiche :
ConcreteObserverA received: 42
ConcreteObserverB received: 42