• 策略模式


    原文链接:http://www.orlion.ga/714/

    解释:

        策略模式帮助构建的对象不必包含本身的逻辑,而是能够根据需要利用其他对象中的算法。

    需求:

        我们本来有一个CD类:

    class CD{
    
    	private $title;
    
    	private $band;
    
    	public function __construct($title , $band) {
    		$this->title = $title;
    		$this->band = $band;
    	}
    
    	public function getAsXml(){
    		$doc = new DOMDocument();
    		$root = $doc->createElement('cd');
    		$root = $doc->appendChild($root);
    		$title = $doc->createElement('title' , $this->title);
    		$title = $root->appendChild($title);
    		$band = $doc->createElement('band' , $this->band);
    		$band = $root->appendChild($band);
    
    		return $doc->saveXML();
    	}
    }

        后来我们想让CD以JSON格式输出,这时可以直接加入一个getAsJson()方法,但是后期我们可能还会让CD以其他各种各样的方式输入,这个CD最后会变得十分臃肿存在很多实例不会调用的方法。这时就可以从类中取出这些方法将其添加到只在需要时创建的具体类中。

        

    代码:

        首先设计一个接口在这个接口里规定要实现方法:

    namespace Strategy;
    
    interface ShowStrategy{
    	function show(CDStrategy $cd);
    }

        然后是XML类:

    namespace Strategy;
    
    class CDAsXMLStrategy implements ShowStrategy{
    
    	public function show(CDStrategy $cd) {
    
    		$doc = new DOMDocument();
    		$root = $doc->createElement('cd');
    		$root = $doc->appendChild($root);
    		$title = $doc->createElement('title' , $cd->title);
    		$title = $root->appendChild($title);
    		$band = $doc->createElement('band' , $cd->band);
    		$band = $root->appendChild($band);
    
    		return $doc->saveXML();
    	}
    }

        接着是JSON类:

    namespace Strategy;
    
    class CDAsJSONStrategy implements ShowStrategy{
    
    	public function show(CDStrategy $cd) {
    		$json = [];
    		$json['cd']['title'] = $cd->title;
    		$json['cd']['band'] = $cd->band;
    
    		return json_encode($json);
    	}
    }

        然后是应用策略模式后的CD类:

    namespace Strategy;
    
    class CDStrategy{
    
    	public $title;
    
    	public $band;
    
    	protected $_strategy;
    
    	public function __construct($title , $band) {
    		$this->title = $title;
    		$this->band = $band;
    	}
    
    	public function setStrategyContext(ShowStrategy $strategy) {
    		$this->_strategy = $strategy;
    	}
    
    	public function get() {
    		return $this->_strategy->show($this);
    	}
    }

        最后是App.php测试下:

    require 'CDStrategy.php';
    require 'ShowStrategy.php';
    require 'CDAsXMLStrategy.php';
    require 'CDAsJSONStrategy.php';
    
    $cd = new StrategyCDStrategy('what?' , 'Simple Plan');
    
    $xml = new StrategyCDAsXMLStrategy();
    $cd->setStrategyContext($xml);
    echo $cd->get();
    echo '<br/>';
    $json = new StrategyCDAsJSONStrategy();
    $cd->setStrategyContext($json);
    echo $cd->get();
  • 相关阅读:
    [leetCode]945.使数组唯一的最小增量
    [leetCode]974. 和可被 K 整除的子数组
    [leetCode]1035.不相交的线
    [leetCode]面试题 10.01. 合并排序的数组
    [leetCode]面试题04.二维数组
    [面试题03]数组中的重复数字
    [leetCode]169.多数元素
    [leetCode]160.相交链表
    [leetCode]155.最小栈
    POJ 2127 Greatest Common Increasing Subsequence
  • 原文地址:https://www.cnblogs.com/orlion/p/5350897.html
Copyright © 2020-2023  润新知