php 对象适配器模式

来源:互联网 发布:电影推荐算法数据集 编辑:程序博客网 时间:2024/04/26 02:09
/** *对象适配器模式 *@author li.yonghuan *@version 2014.02.13 *//** *目标角色 *@author li.yonghuan *@version 2014.02.13 */interface Target {    /**     *源类也有的方法1     */    public function sampleMethod1();    /**     *源类没有的方法2     *     */    public function sampleMethod2();}/** *源角色 *@author li.yonghuan *@version 2014.02.13 */class Adaptee {    /**     *方法1     *     */    public function sampleMethod1() {        echo 'Adaptee sampleMethod1 <br/>';    }}/** *类适配器角色 *@author li.yonghuan *@version 2014.02.13 */class Adapter implements Target {    /**     *adaptee     *@var object Adaptee     */    private $adaptee;    /**     *构造方法     *@param $adaptee Adaptee     *     */    public function __construct( Adaptee $adaptee ) {       $this->adaptee = $adaptee;     }    /**     *方法1     *     */    public function sampleMethod1() {        $this->adaptee->sampleMethod1();    }    /**     *方法2     */    public function sampleMethod2() {        echo 'Adapter sampleMethod2 <br/>';    }}/** *客户端调用 */class Client {    /**     *main program     *     */    public static function main() {        $adaptee = new Adaptee();        $adapter = new Adapter( $adaptee );        $adapter->sampleMethod1();        $adapter->sampleMethod2();    }}Client::main();

0 0
原创粉丝点击