Strategy 策略模式

Strategy 策略模式
將功能變化的部份封裝起來,可以獨立的增加功能或減少功能
重點在物件的選擇上,物件可針對不同狀態策略性的選擇不同功能
參考 http://www.cnblogs.com/ayqy/p/3956704.html#lblModify,改為PHP方式

abstract class Dog
{
    protected $run;
    public abstract function display();
    
    public function getRun()
    {
        return $this->run->run();
    }
    
    public function setRun(IRun $run)
    {
        $this->run = $run;
    }
}

interface IRun
{
    public function run();   
}

class RunSlowly implements IRun
{
    public function run()
    {
        echo "It's running slowly.<br />";
    }   
}

class RunQuickly implements IRun
{
    public function run()
    {
        echo "It's running quickly.<br />";
    }
}

class RedDog extends Dog
{
    public function __construct()
    {
        $this->setRun(new RunSlowly());
    }
    
    public function display()
    {
        echo "It's a Red Dog.<br />";
    }
}

class WhiteDog extends Dog
{
    public function __construct()
    {
        $this->setRun(new RunQuickly());
    }
    
    public function display()
    {
        echo "It's a White Dog.<br />";
    }
}

$redDog = new RedDog();
$redDog->display();
$redDog->getRun();

$whiteDog = new WhiteDog();
$whiteDog->display();
$whiteDog->getRun();

在browser顯示
It’s a Red Dog.
It’s running slowly.
It’s a White Dog.
It’s running quickly.

加入一個新的吠叫功能,而這個功能有大聲叫和小聲叫2種型態

abstract class Dog
{
    protected $run;
    protected $bark;
    public abstract function display();
    
    public function getRun()
    {
        return $this->run->run();
    }
    
    public function setRun(IRun $run)
    {
        $this->run = $run;
    }
    
    public function getBark()
    {
        return $this->bark->bark();
    }
    
    public function setBark(IBark $bark)
    {
        $this->bark = $bark;
    }
}

interface IRun
{
    public function run();   
}

class RunSlowly implements IRun
{
    public function run()
    {
        echo "It's running slowly.<br />";
    }   
}

class RunQuickly implements IRun
{
    public function run()
    {
        echo "It's running quickly.<br />";
    }
}

interface IBark
{
    public function bark();
}

class BarkLoudly implements IBark
{
    public function bark()
    {
        echo "It's Loudly barking.<br />";
    }
}

class BarkWhispered implements IBark
{
    public function bark()
    {
        echo "It's whispered barking.<br />";
    }
}

class RedDog extends Dog
{
    public function __construct()
    {
        $this->setRun(new RunSlowly());
        $this->setBark(new BarkLoudly());
    }
    
    public function display()
    {
        echo "It's a Red Dog.<br />";
    }
}

class WhiteDog extends Dog
{
    public function __construct()
    {
        $this->setRun(new RunQuickly());
        $this->setBark(new BarkWhispered());
    }
    
    public function display()
    {
        echo "It's a White Dog.<br />";
    }
}

$redDog = new RedDog();
$redDog->display();
$redDog->getRun();
$redDog->getBark();

$whiteDog = new WhiteDog();
$whiteDog->display();
$whiteDog->getRun();
$whiteDog->getBark();

在browser顯示
It’s a Red Dog.
It’s running slowly.
It’s Loudly barking.
It’s a White Dog.
It’s running quickly.
It’s whispered barking.