委托模式

委托模式

作者:LAMP小白  点击:1688  发布日期:2012-10-06 09:50:00  返回列表

通过分配或委托至其他对象图,委托设计能够去除核心对象中的判决和复杂的功能性

不要讲委托设计视为条件语句的直接竞争者,相反委托设计模式通过不需要条件语句就可以调用正确功能性的方式来帮助构成体系结构

这就是一个简单的委托模式实现,根据不同的类型去实例化不同的类,然后为用户提供相同的调用方式,返回不同的结果

class mode7
{
    private $_songs;
    private $_typeObject;
    public function __construct($type)
    {
        $this-mio_gt;_songs = array();
        $object = "{$type}PlayList";
        $this-mio_gt;_typeObject = new $object;
    }
    public function addSong($location, $title)
    {
        $song = array('location'=mio_gt;$location, 'title'=mio_gt;$title);
        $this-mio_gt;_songs[] = $song;
    }
    public function getPlayList()
    {
        $playList = $this-mio_gt;_typeObject-mio_gt;getPlayList($this-mio_gt;_songs);
        return $playList;
    }
}



上一篇:装饰器模式 下一篇:快递查询API
0