首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >产品类别责任

问产品类别责任
EN

Stack Overflow用户
提问于 2014-12-16 18:09:16
回答 4查看 178关注 0票数 4

在我的公司,我们有一个非常具体的定价策略:

  • 我们目录中的每个Product都有一个baseUsdPrice,例如,产品Foo的基本美元价格为9.99美元。
  • 这并不意味着那将是你,你将支付9.99美元。我们检查了贵国的价格例外情况,例如GB的价格为8.99美元。
  • 此外,您还可以选择要支付的货币--如果您使用的是GB,您可以使用美元(提到的8.99美元)或本地货币(在本例中是GBP)。
  • 如果你选择用当地货币支付,我们根据固定的价格矩阵计算出相当于8.99美元的英镑(例如,这里是3.99£)。
  • 这就是你要付出的代价。

我应该如何以DDD方式设计我的Product聚合根,使其干净、有凝聚力、解耦和易于更改?

  • paymentPrice是否应该由域服务及其结果作为Product聚合的一部分来计算?如果是这样的话,这意味着我的ProductRepository将有像product(productId, countryId, currency)这样的方法
  • 我应该把所有的计算放在域服务类PaymentPriceCalculator中,并使用像getPaymentPrice(Country, Currency)这样的访问者模式吗?如果我需要使用实体中的paymentPrice来执行一些业务规则检查,该怎么办?

我试图把我的头绕在它周围,我觉得我想得太多了,而且很疼。;)

EN

回答 4

Stack Overflow用户

发布于 2014-12-16 19:50:39

我倾向于你的第二个选择--拥有一个PaymentPriceCalculator。这样,如果您决定更改算法或使用多个算法,您将能够拥有不同的计算器。

  • 我不会把它放在Product总量中,因为价格因国家而异。它还会使产品类更加复杂。从一个领域的角度来看,即使是在不同的国家购买,2种相同的产品不是“平等”的吗?
  • 访客模式不太适合这里。

我可能还有第二个服务,可以将$转换为所需的任何货币。这应该是单独的服务,因为您的域名似乎使用美元的一切,直到结束时,用户需要实际支付的东西。通过这种方式,您还可以添加适用的税收/增值税等,这与按国家计算价格例外情况的逻辑不同。

票数 2
EN

Stack Overflow用户

发布于 2014-12-16 21:45:30

我将声明一个负责计算产品价格的对象,例如:

代码语言:javascript
复制
interface ProductPriceCalculator {

    public function determineProductPrice($productId, Currency $currency = null);
}

由于每个产品都有basePrice,而且我们希望修改它,所以可以这样组织:

代码语言:javascript
复制
class Product {

    private $id;

    /**
     * Initially base price
     * @var Money
     */
    private $price;

    public function modifyPrice(ProductPriceCalculator $calculator, Currency $currency = null) {
        $newPrice = $calculator->determineProductPrice($this->id, $currency);

        if ($newPrice !== null) {
            $this->price = $newPrice;
        }
    }

}

现在,在这种情况下,您需要国家作为价格调整。对我来说,有意义的解决方案是在代码中准确地反映这一点:

代码语言:javascript
复制
class Country implements ProductPriceCalculator {

    private $id;

    /**
     * @var Currency
     */
    private $currency;

    /**
     * Hashmap<String, Money> where product id evaluates to price in $this country
     * @var array
     */
    private $productPrices = array();

    /**
     * @param string $productId
     * @param Currency $currency
     * @return Money
     */
    public function determineProductPrice($productId, Currency $currency = null) {
        if (array_key_exists($productId, $this->productPrices)) {
            $productPrice = clone $this->productPrices[$productId];

            if ($currency !== null) {
                $currency = $this->currency;
            }

            return $productPrice->convertTo($currency);
        }
    }

}

现在要支持货币和货币逻辑:

代码语言:javascript
复制
class Money {

    private $value;
    private $currency;

    public function __construct($amount, Currency $currency) {
        $this->value = $amount;
        $this->currency = $currency;
    }

    public function convertTo(Currency $newCurrency) {
        if (!$this->currency->equalTo($newCurrency)) {
            $this->value *= $newCurrency->ratioTo($this->currency);
            $this->currency = $newCurrency;
        }
    }

}

class Currency {

    private $code;
    private static $conversionTable = array();

    public function equalTo(Currency $currency) {
        return $this->code == $currency->code;
    }

    public function ratioTo(Currency $currency) {
        return self::$conversionTable[$this->code . '-' . $currency->code];
    }

}

最后,客户看起来会是这样的:

代码语言:javascript
复制
class SomeClient {

    public function someAction() {
        //initialize $product (it has the base price)
        //initialize $selectedCountry with prices for this product
        //initialize $selectedCurrency

        $product->modifyPrice($selectedCountry, $selectedCurrency);

        //here $product contains the price for that country
    }

}
票数 0
EN

Stack Overflow用户

发布于 2014-12-17 02:45:03

同意@dkatzel的说法,汇总不是一个进行计算的好地方。

假设产品保存了计算:

代码语言:javascript
复制
product.paymentPrice(countryId, currency, fixedPriceMatrix)

要测试这一点,您需要在每个测试用例中构建一个产品,尽管有些情况只关注货币的选择。

代码语言:javascript
复制
countryId = ...
currency = ...
fixedPriceMatrix = ...
basePrice = ...
countryPrice = ...
product = new Product(id, basePrice, countryPrice...)
paymentPrice = product.paymentPrice(countryId, currency, fixedPriceMatrix)

在实际项目中,聚合包含许多信息(用于不同的目的),这使得在测试中设置它们相对比较困难。

测试当前计算的容易方法是使用值对象PaymentPrice

代码语言:javascript
复制
//in unit test
paymentPrice = new PaymentPrice(basePriceForYourCountry, currency, fixedPriceMatrix)
value = paymentPrice.value()

//the product now holds countryPrice calculation
countryPrice = new Product(id, basePrice).countryPrice(countryId);

您可以使用PaymentPriceCalculator作为无状态域服务作为工厂:

代码语言:javascript
复制
class PaymentPriceCalculator {
    PaymentPrice paymentPrice(product, countryId, currency) {
        fixedPriceMatrix = fixedPriceMatrixStore.get()
        return new PaymentPrice(product.countryPrice(countryId), currency, fixedPriceMatrix())
    }
}

可能的变化可能是:

  1. 算法更改(您可以将PaymentPrice提取为超类,并为不同的算法引入各种子类)
  2. 为用户提供更多选项。这可能会破坏现有的方法签名以添加更多的参数。您可以引入一个参数对象来保存countryId、通货和其他。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27511141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档