策略模式

策略模式

作者:LAMP小白  点击:1745  发布日期:2012-10-08 23:50:00  返回列表
策略模式帮助构建的对象不必自身包含逻辑,而是能够利用其他对象中的算法
与委托模式类似他都是根据需要new 出其他类,不过他只是获取其他对象为他计算出的结果

比如这个,就可以根据需要选择是要XML还是JSON

mio_lt;?php
    class CDusesStrategy
    {
        public $title = '';
        public $band = '';
        protected $_strategy;
        public function __construct($title, $band)
        {
            $this-mio_gt;title = $title;
            $this-mio_gt;band = $band;
        }
        public function set($obj)
        {
            $this-mio_gt;_strategy = $obj;
        }
        public function get()
        {
            return $this-mio_gt;_strategy-mio_gt;get($this);
        }
    }
    class cdAsXML
    {
        public function get(CDusesStrategy $cd)
        {
            return 'XML';
        }
    }
    class cdAsJson
    {
        public function get(CDusesStrategy $cd)
        {
            return 'json';
        }
    }
?mio_gt;


上一篇:单元素模式 下一篇:快递查询API
0