国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

PHP實(shí)現(xiàn)的策略模式簡單示例

2024-05-04 23:59:45
字體:
供稿:網(wǎng)友

本文實(shí)例講述了PHP實(shí)現(xiàn)的策略模式。分享給大家供大家參考,具體如下:

比如說購物車系統(tǒng),在給商品計(jì)算總價(jià)的時(shí)候,普通會員肯定是商品單價(jià)乘以數(shù)量,但是對中級會員提供8者折扣,對高級會員提供7折折扣,這種場景就可以使用策略模式實(shí)現(xiàn):

<?php/** * 策略模式實(shí)例 * *///抽象策略角色《為接口或者抽象類,給具體策略類繼承》interface Strategy{  public function computePrice($price);}//具體策略角色-普通會員策略類class GenernalMember implements Strategy{  public function computePrice($price)  {    return $price;  }}//具體策略角色-中級會員策略類class MiddleMember implements Strategy{  public function computePrice($price)  {    return $price * 0.8;  }}//具體策略角色-高級會員策略類class HignMember implements Strategy{  public function computePrice($price)  {    return $price * 0.7;  }}//環(huán)境角色實(shí)現(xiàn)類class Price{  //具體策略對象  private $strategyInstance;  //構(gòu)造函數(shù)  public function __construct($instance)  {    $this->strategyInstance = $instance;  }  public function compute($price)  {    return $this->strategyInstance->computePrice($price);  }}//客戶端使用$p = new Price(new HignMember());$totalPrice = $p->compute(100);echo $totalPrice; //70?>

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 文昌市| 合肥市| 漠河县| 临夏市| 呼图壁县| 买车| 法库县| 安国市| 望谟县| 灵璧县| 旌德县| 资兴市| 金平| 浠水县| 稷山县| 牙克石市| 耒阳市| 米泉市| 吉首市| 宁阳县| 台南市| 广汉市| 广南县| 潮州市| 肇州县| 楚雄市| 上饶县| 仁怀市| 贡山| 顺义区| 庆安县| 九台市| 濮阳市| 文昌市| 普兰店市| 平定县| 长泰县| 聊城市| 沅陵县| 龙门县| 新晃|