《大话设计模式》策略模式-商场促销场景-PHP版代码

Standard

策略模式
商场促销场景

<?php
/**
 * 策略模式 商场促销场景
 * @author 叶茂盛 anson.ye#gmail.com  http://yemaosheng.com
 */
abstract class CashSuper{
        public abstract function acceptCash($money);
}
class CashNormal extends CashSuper{
        public function acceptCash($money){
                return $money;
        }
}
class CashRebate extends CashSuper{
        private $_moneyRebate;
        public function __construct($moneyRebate){
                $this->_moneyRebate = $moneyRebate;
        }
        public function acceptCash($money){
                return $money * $this->_moneyRebate;
        }
}
class CashReturn extends CashSuper{
        private $_moneyCondition;
        private $_moneyReturn;
        public function __construct($moneyCondition, $moneyReturn){
                $this->_moneyCondition = $moneyCondition;
                $this->_moneyReturn = $moneyReturn;
        }
        public function acceptCash($money){
                if( $money >= $this->_moneyCondition )
                        $result = $money - floor($money / $this->_moneyCondition) * $this->_moneyReturn;
 
                return $result;
        }
}
 
class CashContext{
        private $_cs;
 
        public function __construct(CashSuper $csuper){
                $this->_cs = $csuper;
        }
        public function GetResult($money){
                return $this->_cs->acceptCash($money);
        }
}
 
//简单工厂
class CashContextFactory{
        private $_cs;
 
        public function __construct($type){
                switch ($type) {
                case "a":
                    $this->_cs = new CashNormal();
                    break;
                case "b":
                    $this->_cs = new CashReturn("300", "100");
                    break;
                case "c":
                    $this->_cs = new CashRebate("0.8");
                    break;
                }
        }
        public function GetResult($money){
                return $this->_cs->acceptCash($money);
        }
}
 
class Main{
        public static function run_Strategy(){
                fwrite(STDOUT, "数量: ");
                $number = trim(fgets(STDIN));
 
                fwrite(STDOUT, "单价: ");
                $price = trim(fgets(STDIN));
 
                fwrite(STDOUT, "折扣: a = 原价 b = 返利 c = 8折 \nEnter your discount: ");
                $discount = trim(fgets(STDIN));
 
                switch ($discount) {
                case "a":
                    $cc = new CashContext( new CashNormal() );
                    break;
                case "b":
                    $cc = new CashContext( new CashReturn("300", "100") );
                    break;
                case "c":
                    $cc = new CashContext( new CashRebate("0.8") );
                    break;
                }
 
                echo "\n======帐单======\n数量:" .$number. "\n单价". $price. "\n折后总价:". $cc->GetResult($number*$price);
        }
        public static function run_StrategyFactory(){
                fwrite(STDOUT, "数量: ");
                $number = trim(fgets(STDIN));
 
                fwrite(STDOUT, "单价: ");
                $price = trim(fgets(STDIN));
 
                fwrite(STDOUT, "折扣: a = 原价 b = 返利 c = 8折 \nEnter your discount: ");
                $discount = trim(fgets(STDIN));
 
                $cc = new CashContextFactory($discount);
 
                echo "\n======帐单======\n数量:" .$number. "\n单价". $price. "\n折后总价:". $cc->GetResult($number*$price);
        }
}
Main::run_StrategyFactory();
?>
//php Strategy.php 
//数量: 10
//单价: 120
//折扣: a = 原价 b = 返利 c = 8折 
//Enter your discount: c
 
//======帐单======
//数量:10
//单价120
//折后总价:960

转载请注明出处 http://yemaosheng.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.