策略模式和工厂模式在促销系统下的应用

策略模式和工厂模式在促销系统下的应用

标签: 设计模式 策略模式 工厂模式 促销系统 php


设计模式为我们提供了在某些应用场景下特定问题的解决方案,今天我们利用策略模式和工厂模式实现一个简单的促销系统。
实现语言:PHP
我们先来回顾一下策略模式和工厂模式的基本知识

策略模式

Gof 对策略模式的作用的说明如下

定义一系列的算法,把他们一个个封装起来,并且使他们可以相互替换。本模式使得算法可独立于他的客户而变化。

策略模式对算法进行抽象,使得类可以方便地实现不同的算法,而调用他的客户端完全感觉不出来。

工厂模式

Gof 整理的工厂模式主要有抽象工厂模式和工厂方法模式,大家还经常提到一种简单工厂模式,工厂模式将类的创建进行封装,使得类的创建和使用可以相互分离,独立变化。

问题系统描述

要实现一个促销系统,实现对单件产品和购物车产品集合的促销。针对单件产品,促销策略有降价、打折、赠品,限制条件有无条件限制、商品满足一定价格;针对购物车产品集合,促销策略有打折、降价、赠品、低价免单、免邮,限制条件有无条件限制、商品集合总价满一定价格、商品集合满一定数量。

基本数据表及类设计

促销系统核心数据表设计

  1. promotion表,存储信息包括促销类别(针对单件产品的促销活动还是针对产品集合的促销活动),优惠策略,限制条件。

促销系统核心类设计

  1. Promotion 类,对应promotion数据表,实现某个限制条件下针对单件产品或产品集合的某个促销策略。
  2. Benefit 系列类,提供在产品和产品集合上不同的促销优惠的算法。
  3. Checkout 系列类,提供在产品和产品集合上不同的限制条件的算法。

除了以上核心类类外,还有以下基础类,比如Product(产品类,实现一件产品的具体功能)、Collection(集合类,某些类对象的集合,可以继承Collection实现比如产品集合等具体集合类)。

促销系统设计思路

通过以上分析,我们知道实现促销的对象有两个:单件产品和产品集合;主要功能也有两个:判断是否满足限制条件和实现某种促销优惠。限制条件和促销优惠这两个功能具有稳定性,具体实现算法具有可变性。这样我们可以针对限制条件和促销优惠抽象出来其接口,使用策略模式进行封装,使得判断条件和促销优惠的算法可以独立于客户端进行变化和增减,并通过工厂模式创建具体算法实例。我们还应该设计针对 Promotion 类接口,抽象出来提供给客户端调用算法的方法。

促销系统实现

接口设计

interface Promotion
{   
    /*
     * Promotion 类的接口,标注 Promotion 类向系统提供的具体功能
     */
     
    //返回促销策略的类别信息
    public function getBenefit();
    //返回促销策略的值
    public function getBenefitValue();
    //返回限制条件的信息
    public function getLimit();
    //返回限制条件的值
    public function getLimitValue();
    //创建此次促销产生的优惠信息的对象
    public function makeBenefit($attribute);

    //判断某个产品集合是否满足促狭的限制条件 
    public function itemsCheckout(Collection $items, $limitValue);
    //判断某个产品是否满足促销的限制条件
    public function productCheckout(Product $product, $limitValue);
    //针对某个产品集合计算促销策略
    public function itemsPromotionCalculate(Collection $items);
    //针对某个产品计算促销策略
    public function productPromotionCalculate(Product $product);
}

interface PromotionBenefit{
    /*
     * 促销优惠策略的接口,提供针对产品集合和单件产品的促销优惠的方法 
     */
     
    // 计算产品集合的促销优惠
    public function itemsPromotionCalculate(Promotion $promotion, Collection $items);
    // 计算单件产品的促销优惠
    public function productPromotionCalculate(Promotion $promotion, Product $product);
}

interface PromotionCheckout{
    /*
     * 促销限制条件的接口,提供针对产品集合和单件产品的判断限制条件方法
     */
    
    //判断产品集合的限制条件
    public function itemsCheckout(Collection $items, $limitValue);
    //判断单件产品的限制条件
    public function productCheckout(Product $product, $limitValue);
}

限制条件策略的实现

根据上面的限制条件策略的接口,接下来我们来实现无限制条件、满一定价格、满一定数目这些限制条件的策略

/*
* 实现无条件限制策略,因为无条件限制,所以接口的两个方法均直接返回true
*/
class CheckoutNone implements PromotionCheckout{
    public function itemsCheckout(Collection $items, $limitValue)
    {
        return true;
    }

    public function productCheckout(Product $product, $limitValue)
    {
        return true;
    }
}

/*
* 实现对单件商品和商品集合满一定价格的限制条件策略
*/
class CheckoutFullPrice implements PromotionCheckout{
    public function itemsCheckout(Collection $items, $limitValue)
    {
        return $items->reduce(function($amount, $item){
            return $amount + $item->price * $item->qty;
        }, 0) >= $limitValue;
    }

    public function productCheckout(Product $product, $limitValue)
    {
        return $product->getBasePrice() >= $limitValue;
    }
}

/*
* 实现对商品集合满一定数目的限制条件策略
*/
class CheckoutFullNumber implements PromotionCheckout{
    public function itemsCheckout(Collection $items, $limitValue)
    {
        return $items->sum('qty') >= $limitValue;
    }

    public function productCheckout(Product $product, $limitValue)
    {
        //因为单件商品没有满足一定价格的限制条件,所以直接返回false
        return false;
    }
}

限制条件策略工厂的实现

我们把限制条件策略类的实例化工作教给工厂类来实现,我们来看一下限制条件策略工厂类的实现

class PromotionCheckoutFactory{
    private static $_instances;

    /**
     * @return PromotionCheckout
     */
    public static function makePromotionCheckout($limit){
        if(!isset(self::$_instances[$limit])){
            switch($limit){
                case Promotion::LIMIT_NONE : self::$_instances[$limit] = new CheckoutNone(); break;
                case Promotion::LIMIT_FULL_PRICE : self::$_instances[$limit] = new CheckoutFullPrice();break;
                case Promotion::LIMIT_FULL_NUMBER : self::$_instances[$limit] = new CheckoutFullNumber(); break;
            }
        }
        return isset(self::$_instances[$limit]) ? self::$_instances[$limit] : null;
    }
}

促销优惠策略的实现

实现了促销优惠策略的接口,接下来我们就来实现降价、打折、低价免单、赠品、免邮等具体促销优惠策略。

class BenefitPriceReduction implements PromotionBenefit{
    public function itemsPromotionCalculate(Promotion $promotion, Collection $items)
    {
        /*
        * 实现产品集合的降价促销策略
        */ 
        if($promoiton->getBenefit() != Promotion::BENEFIT_PRICE_REDUCTION){
            return false;
        }

        $size = max(count($promoiton->getBenefitValue()), count($promoiton->getLimitValue()));
        $benefitValues = array_pad($promoiton->getBenefitValue(), $size, 0);
        $limitValues = array_pad($promoiton->getLimitValue(), $size, false);
        $benefits = array_combine($limitValues, $benefitValues);
        
        /*
        * 促销支持组合设定,比如这里可以表示满100减10,满200减25,满300减50,则$benefit的值为 
        *[
        *    100 => 10,
        *    200 => 25,
        *    300 => 50
        *]
        */
        
        $attribute = false;
        foreach($benefits as $limitValue => $benefitValue){
            $benefitValue = floatval($benefitValue);
            if($promoiton->itemsCheckout($items, $limitValue) && $benefitValue > 0){
                $goodsAmount = $items->reduce(function($amount, $item){
                    return $amount + $item->price * $item->qty;
                }, 0);

                $data = [
                    'base_price' => $goodsAmount,
                    'old_price' => $goodsAmount,
                    'new_price' => round($goodsAmount - $benefitValue, 2),
                    'benefit_price' => $benefitValue,
                ];
                if(!$attribute || $attribute['benefit_price'] < $data['benefit_price']){
                    $attribute = $data;
                    $attribute['select_limit_value'] = $limitValue;
                    $attribute['select_benefit_value'] = $benefitValue;
                }
            }
        }

        if($attribute){
            return $promoiton->makeBenefit($attribute);
        }
        return false;
    }

    public function productPromotionCalculate(Promotion $promoiton, Product $product)
    {
        /*
        * 实现单件产品的降价促销策略
        */ 
        if($promoiton->getBenefit() != Promotion::BENEFIT_PRICE_REDUCTION){
            return false;
        }

        $size = max(count($promoiton->getBenefitValue()), count($promoiton->getLimitValue()));
        $benefitValues = array_pad($promoiton->getBenefitValue(), $size, 0);
        $limitValues = array_pad($promoiton->getLimitValue(), $size, false);
        $benefits = array_combine($limitValues, $benefitValues);

        $attribute = false;
        foreach($benefits as $limitValue => $benefitValue){
            $benefitValue = floatval($benefitValue);
            if($promoiton->productCheckout($product, $limitValue) && $benefitValue > 0){
                $data = [
                    'base_price' => $product->getBasePrice(),
                    'old_price' => $product->getBasePrice(),
                    'new_price' => round( $product->getBasePrice() - $benefitValue, 2),
                    'benefit_price' =>$benefitValue,
                ];
                if(!$attribute || $attribute['benefit_price'] < $data['benefit_price']){
                    $attribute = $data;
                    $attribute['select_limit_value'] = $limitValue;
                    $attribute['select_benefit_value'] = $benefitValue;
                }
            }
        }

        if($attribute){
            return $promoiton->makeBenefit($attribute);
        }
        return false;
    }
}

class PromotionDiscount implements PromotionBenefit{
    public function itemsPromotionCalculate(Promotion $promotion, Collection $items)
    {
        /*
        * 实现针对组合产品的打折优惠
        */
        ...
    }

    public function productPromotionCalculate(Promotion $promotion, Product $product)
    {
        /*
        * 实现针对单件产品的打折优惠
        */
        ...   
    }
}

class BenefitLowerFree implements PromotionBenefit{
    public function itemsPromotionCalculate(Promotion $promotion, Collection $items)
    {
        /*
        * 实现针对产品集合的低价免单
        */
        ...
    }

    public function productPromotionCalculate(Promotion $promotion, Product $product)
    {
        /*
        * 单件产品不支持低价免单策略
        */
        return false;
    }
}

class BenefitFreeShipping implements PromotionBenefit{
    public function itemsPromotionCalculate(Promoiton $promotion, Collection $items)
    {
        /*
        * 判断产品集合是否满足免邮限制条件
        */
        ...
    }

    public function productPromotionCalculate(Promotion $promotion, Product $product)
    {
        /*
        * 单件产品不支持免邮策略
        */
        return false;
    }
}

class BenefitGift implements PromotionBenefit{
    /*
    * @return false | $gift
    * false 此产品集合不满足赠品条件
    * $gift 此产品集合此次促销的赠品
    */
    public function itemsPromotionCalculate(Promotion $promotion, Collection $items)
    {
        /*
        * 实现产品集合的赠品优惠
        */
        ...
    }
    
    /*
    * @return false | $gift
    * false 此产品不满足赠品条件
    * $gift 此产品集合此次促销的赠品
    */
    public function productPromotionCalculate(Promotion $promotion, Product $product)
    {
        /*
        * 实现单件产品的赠品优惠
        */
        ...
    }
}

优惠策略工厂的实现

我们实现了具体的促销优惠策略,接下来实现优惠策略的工厂

class PromotionBenefitFactory{
    private static $_instances;
    /**
     * @return PromotionBenefit
     */
    public static function makePromotionStrategy($benefit)
    {
        if(!isset(self::$_instances[$benefit])){
            switch($benefit){
                case Promotion::BENEFIT_DISCOUNT : self::$_instances[$benefit] = new BenefitDiscount(); break;
                case Promotion::BENEFIT_PRICE_REDUCTION : self::$_instances[$benefit] = new BenefitPriceReduction(); break;
                case Promotion::BENEFIT_LOWER_FREE : self::$_instances[$benefit] = new BenefitLowerFree(); break;
                case Promotion::BENEFIT_FREE_SHIPPING : self::$_instances[$benefit] = new BenefitFreeShipping(); break;
                case Promotion::BENEFIT_GIFT : self::$_instances[$benefit] = new BenefitGift(); break;
            }
        }
        return isset(self::$_instances[$benefit]) ? self::$_instances[$benefit] : null;
    }
}

Promotion 类的实现

Promotion 类应该是对应 promotion 表的 ORM, 除此之外,Promotion 类还应该实现 Promotion 接口,实现促销系统所需的方法

/*
* Note:PromotionInterface 指的是 Promotion 接口,因为 PHP 不允许类和接口名字一样,所以需要将 Promotion 接口 use 的时候 as 成 PromotionInterface
*/
class Promotion extends Model implements PromotionInterface
{
    protected $table = 'promotions';
    protected $primaryKey = 'pmid';

    /**
     * @var PromotionBenefit
     */
    private $promotionStrategy;

    /**
     * @var PromotionCheckout
     */
    private $promotionCheckout;

    const TYPE_PRODUCT = 'product';
    const TYPE_CART = 'cart';

    const LIMIT_NONE = 'no_limit';
    const LIMIT_FULL_PRICE = 'full_price';
    const LIMIT_FULL_NUMBER = 'full_number';

    const BENEFIT_DISCOUNT = 'discount';
    const BENEFIT_PRICE_REDUCTION = 'price_reduction';
    const BENEFIT_LOWER_FREE = 'lower_free';
    const BENEFIT_FREE_SHIPPING = 'free_shipping';
    const BENEFIT_GIFT = 'gift';

    public function __construct()
    {
        parent::__contruct();
        $this->promotionStrategy = PromotionStrategyFactory::makePromotionStrategy($this->getBenefit());
        $this->promotionCheckout = PromotionCheckoutFactory::makePromotionCheckout($this->getLimit());
    }
    
    public function getBenefit(){
        return $this->benefit;
    }
    /**
     * @return array
     */
    public function getBenefitValue()
    {
        return $this->benefit_value;
    }

    public function getLimit()
    {
        return $this->limit;
    }

    /**
     * @return array
     */
    public function getLimitValue()
    {
        return $this->limit_value;
    }

    public function makeBenefit($attribute)
    {
        return new Benefit($attribute, $this);
    }

    public function itemsPromotionCalculate(Collection $items)
    {
        if($this->promotionStrategy && $this->type == self::TYPE_CART){
            return $this->promotionStrategy->itemsPromotionCalculate($this, $items);
        }
    }

    public function productPromotionCalculate(Product $product)
    {
        if($this->promotionStrategy && $this->type == self::TYPE_PRODUCT){
            return $this->promotionStrategy->productPromotionCalculate($this, $product);
        }
    }

    public function itemsCheckout(Collection $items, $limitValue)
    {
        if($this->promotionCheckout){
            return $this->promotionCheckout->itemsCheckout($items, $limitValue);
        }
    }

    public function productCheckout(Product $product, $limitValue)
    {
        if($this->promotionCheckout){
            return $this->promotionCheckout->productCheckout($product, $limitValue);
        }
    }
}

是否应该使用访问者模式

至此,我们主要利用了策略模式的工厂模式实现了促销系统的主要功能。在实现的过程中我考虑过需不需要使用访问者模式来对代码进行进一步抽象,因为访问者模式的作用是

表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变的类的前提下定义作用于这些元素的新操作。

因为我们发现的主要作用对象是单件商品和商品集合,然后我们定义了这两个作用对象上的操作:判断限制条件和进行促销优惠。似乎是可以使用访问者模式进行抽象,将判断限制条件和进行促销优惠的的接口合并成对操作对象的接口,通过在此接口上的扩展实现对判断限制条件和进行促销优惠的扩展。这样做貌似也可以行得通,不过在实例中,将限制条件和促销优惠进行更加具体的抽象,代码更加容易理解,也更加易读,在操作对象上面操作行为的类别相对固定的情况下,使用访问者模式有点过度抽象。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,636评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,890评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,680评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,766评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,665评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,045评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,515评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,182评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,334评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,274评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,319评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,002评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,599评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,675评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,917评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,309评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,885评论 2 341

推荐阅读更多精彩内容

  • 第四章:协议与分类 23.通过委托协议和数据源协议与对象间通信 委托模式:用于遵循协议的对象对自身传递信息(对内接...
    iOS打怪升级阅读 195评论 0 0
  • 1.背景介绍 团队开发过程中,如果编码不规范,很容易造成出现bug之后难以维护等问题 所以一个具有普适性的编码规范...
    大猫_9b60阅读 979评论 0 0
  • “我朋友今年交的个税金额都是我的工资收入了!” “免征额提高了,纳税光荣这句话我就没资格了啊!” “我的收入也是辛...
    颜家小七阅读 142评论 0 1
  • 记录自己对于母亲的思想,反抗是在尝试找到自己,修复自己,至于方法,在反抗中反思,不断优化方式。 好的方面 正直 勤...
    nancy的成长阅读 190评论 0 0