php设计模式之适配器模式

适配器模式分为两种分别是类适配器和对象适配器

借鉴http://blog.csdn.net/hguisu/article/details/7527842

适用性:

  • 你想使用一个已经存在的类,而它的接口不符合你的需求。
  • 你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类协同工作。
  • (仅适用于对象适配器)你想使用一些已经存在的子类,但是不可能对每一个都进行子类化以匹配它们的接口。对象适配器可以适配它的父类接口。即仅仅引入一个对象,并不需要额外的指针以间接取得adaptee。

原理:

将要用到的角色分为以下几种:target最早实现的接口,adaptee改善之后的接口,adapter对target和adapter进行适配,client调用接口

类适配器:

<?php  
/** * 类适配器模式 * @author mtg * */  

/** * 目标角色 * @version 1.0 */  
class Target {  

    /** * 这个方法将来有可能改进 */  
    public function hello(){  
        echo 'Hello ';  
    }  

    /** * 目标点 */  
    public function world(){  
        echo 'world';  
    }  
}  

/** * Client程序 调用target所定义的接口 * */  
class Client {  

    public static function main() {  
        $Target = new Target();  
        $Target->hello();  
        $Target->world();  

    }  

}  
Client::main();  
?>  

Target已经明确指出hello()方法会在未来的版本中改进,甚至不被支持或者淘汰。接下来,现在假设第二版的Target已经发布。一个全新的greet()方法代替了hello()。

<?php  
/** * 类适配器模式 * @author guisu * */  

/** * 目标角色 * @version 2.0 */  
class Target {  

    /** * 这个方法将来有可能继续改进 */  
    public function greet(){  
        echo 'Greet ';  
    }    
    public function world(){  
        echo 'world';  
    }  
}  

如果我们继续使用原来的client代码,肯定会报错,找不到hello方法。
针对API“升级”的解决办法就是创建一个适配器(Adapter)。

<?php  
/** * 类适配器模式 * @author mtg * */  

/** * 目标角色 * @version 2.0 */  
interface Target {  

    /** *将原本的target类改造为一个接口 */  
    public function hello();  

    /** * 目标点 */  
    public function world();  
}  

/** * 源角色:被适配的角色,新的接口类 */  
class Adaptee {  
    /** * 源类含有的方法 */  
    public function world() {  
        echo ' world <br />';  
    }  

    /** * 加入新的方法 */  
    public function greet() {  
        echo ' Greet ';  
    }  
}  

/** * 类适配器角色 即继承adaptee又实现target,现在的adapter有三个方法,分别是world(),greet(),和hello()方法,并且hello方法就是新增加的greet方法,此时,客户端代码是不需要改变的,只需将需要调用的类替换掉 */  
class Adapter extends Adaptee implements Target {  

    /** * 重写事项的接口中的hello方法 */  
    public function hello() {  
       parent::greet();  
    }  

}  
/** * 客户端程序 * */  
class Client {  
    public static function main() {  
        $adapter = new Adapter();  
        $adapter->hello();  
        $adapter->world();  
    }  
}  
Client::main();  
?>  

对象适配器

<?php  
/** * 对象适配器模式 * @author guisu * */  

/** * 目标角色 * @version 2.0 */  
interface Target {  

    /** * 源类的方法:这个方法将来有可能继续改进 */  
    public function hello();  

    /** * 目标点 */  
    public function world();  
}  

/** * 源角色:被适配的角色 */  
class Adaptee {  
    /** * 源类含有的方法 */  
    public function world() {  
        echo ' world <br />';  
    }  

    /** * 加入新的方法 */  
    public function greet() {  
        echo ' Greet ';  
    }  
}  

/** * 在adapter中持有adptee的对象,此时adapter类就可以使用adaptee的接口,adapter类再通过接口转换,使客户端可以直接使用 */  
class Adapter  implements Target {  

    private $_adaptee;  

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

    /** * 通过接口转换,将greet()方法转换为hello() */  
    public function hello() {  
       $this->_adaptee->greet();  
    }  

    /** * 通过接口转换,将旧版本的world()方法转换为新版本的world的方法 */  
    public function world() {  
       $this->_adaptee->world();  
    }  
}  
/** * 客户端程序 * */  
class Client {  

    /** * Main program. */  
    public static function main() {  
        $adaptee = new Adaptee();  
        $adapter = new Adapter($adaptee);  
        $adapter->hello();  
        $adapter->world();  
    }  
}  

Client::main();  
?>  

你可能感兴趣的:(设计模式,PHP,适配器)