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

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

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

设计模式为我们提供了在某些应用场景下特定问题的解决方案,今天我们利用策略模式和工厂模式实现一个简单的促销系统。

实现语言:PHP

我们先来回顾一下策略模式和工厂模式的基本知识

策略模式

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

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

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

工厂模式

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

问题系统描述

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

基本数据表及类设计

促销系统核心数据表设计

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

促销系统核心类设计

Promotion 类,对应promotion数据表,实现某个限制条件下针对单件产品或产品集合的某个促销策略。

Benefit 系列类,提供在产品和产品集合上不同的促销优惠的算法。

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);

}

}

}

是否应该使用访问者模式

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值