設計模式php實例:適配器模式

適配器模式將一個類的換成客戶端需要的另一個接口,使原本不兼容的接口能一起工作。一個現實中的例子,假如屋裏牆上只有兩項插口,而風扇有三個插頭,這時需要再拿一個移動插線板,插線板接入牆壁插口的一段是兩項插頭,插線板另外一段能夠提供三項插口給風扇,這時候插線板起到了一個適配器的作用。

在應用中,適配器模式分爲類適配器和對象適配器。


類適配器


類適配器中適配器繼承原有的Adaptee類,自己實現原類沒有的操作。

interface ITarget
{
    function operation1();
    function operation2();
}

interface IAdaptee
{
    function operation1();
}

class Adaptee extends IAdaptee
{
    public  function operation1()
    {
        echo "原方法";
    }
}

class Adapter extends Adaptee implements IAdaptee, ITarget
{
    public  function operation2()
    {
        echo "適配方法";
    }
}

class Client
{
    public  function test()
    {
        $adapter = new Adapter();
        $adapter->operation1();//原方法
        $adapter->operation2();//適配方法
    }
}


對象配器


類適配器使用的是繼承模式,而對象適配器使用的是組合模式,將adaptee作爲adapter的一個引用。

interface ITarget
{
    function operation1();
    function operation2();
}

interface IAdaptee
{
    function operation1();
}

class Adaptee extends IAdaptee
{
    public  function operation1()
    {
        echo "原方法";
    }
}

class Adapter implements  ITarget
{
    private $adaptee;

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

    public  function operation1()
    {
         return $this->adaptee->operation1();
    }

    public  function operation2()
    {
        echo "適配方法";
    }
}


class Client
{
    public  function test()
    {
        $adapter = new Adapter(new Adaptee(null));
        $adapter->operation1();//原方法
        $adapter->operation2();//適配方法
    }
}
由於組合在耦合性上小於繼承,對象適配器顯得更加靈活,當然它的缺點是增加代碼量。 需要重寫adapee中的方法的數量太大的話,可以考慮在adapter中使用__call方法委託adapee取得客戶端調用的方法
    public function __call($func, $args)
    {
        if (is_callable(array($this->adaptee, $func))) {
            return $this->adaptee->$func($args);
        }
        trigger_error('*********', E_USER_ERROR);
    }



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章