php之策略模式
策略模式:封装算法,选择所用具体实现的置业由客户对象承担。
将算法具体类,然后互相替换,不影响客户。
<?php /** * 设计模式之策略模式 * User: 小狗蛋儿 * Date: 2017/11/20 * Time: 20:44 * * 各个算法之间相互替换,不会影响到使用算法的客户 */ /** * Class Xianjin * 现金收费类 */ abstract class Xianjin{ abstract function Jine($rmb); } class A extends Xianjin{ public function Jine($rmb) { echo "0"; return $rmb; } } class B extends Xianjin{ public $zhe="0.8"; public function Jine($rmb) { echo "1"; return $rmb*$this->zhe; } } class C extends Xianjin{ public $a1=300; public $a2=60; public function Jine($rmb) { echo "3"; $result=$rmb; if ($rmb>=$this->a1){ $result=$rmb-floor($rmb/$this->a1)*$this->a2; } return $result; } } class ContentCax{ public $ceshi; public function __Construct($types){ switch ($types){ case "0": $this->ceshi=new A(); break; case "1": $this->ceshi=new B(); break; case "2": $this->ceshi=new C(); break; } } public function GetRestult($rmb){ return $this->ceshi->Jine($rmb); } } /*** * 客户类 */ class ssd{ public function ceshi(){ $a=new ContentCax("2"); $a->GetRestult("100"); } } $ssd=new ssd(); $ssd->ceshi();