php电商优惠券规则引擎代码
代码语言:php
所属分类:其他
代码描述:php电商优惠券规则引擎代码, 使用 Specification 模式封装"条件" (Condition),避免在业务层写 if。 使用 Strategy 模式封装"计算" (Action)。 优惠券对象只是条件与动作的组合。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<?php
/**
* PHP 5.6 电商优惠券规则引擎示例
*
* 核心思想:
* 1. 使用 Specification 模式封装"条件" (Condition),避免在业务层写 if。
* 2. 使用 Strategy 模式封装"计算" (Action)。
* 3. 优惠券对象只是条件与动作的组合。
*/
// -----------------------------------------------------------------------------
// 1. 基础数据对象 (DTO)
// -----------------------------------------------------------------------------
class CartItem {
public $id;
public $price;
public $quantity;
public $category;
public function __construct($id, $price, $quantity, $category) {
$this->id = $id;
$this->price = $price;
$this->quantity = $quantity;
$this->category = $category;
}
public function getTotal() {
return $this->price * $this->quantity;
}
}
class Cart {
public $items = array();
public function addItem(CartItem $item) {
$this->items[] = $item;
}
public function getSubtotal() {
$total = 0;
foreach ($this->items as $item) {
$total += $item-&.........完整代码请登录后点击上方下载按钮下载查看















网友评论0