货币是值对象还是枚举?

问题描述

这就是我今天问自己的问题。
如果货币是 ISO-4217 中的 3 个字母值,并且我们不想将其表示为原始字符串而是将其表示为对象,那么该应该实现为 VO 还是 Enum?请在 Money 类的上下文中考虑我的问题,当然这是一个值对象。货币是否可以声明为枚举,但即使也被视为值对象?或者这取决于上下文 - 在一种货币中应该是 VO,在另一种货币中应该是 Enum?

PHP 示例:

// Value Object
class Currency
{
    private $code;

    public function __construct(string $code)
    {
        ISO4217::validateCurrency($code); // some class validating $code
        $this->code = $code;
    }

    public function getCode(): string
    {
        return $this->code;
    }
}

// Enum
class Currency extends SomeBaseEnum
{
    public const EUR = 'EUR';
    public const USD = 'USD';
    // and so on
}

// Value Object
class Money
{
    // fields here
 
    // Currency here should be VO or Enum?
    public function __construct(int $amount,Currency $currency)
    {
        $this->amount = $amount;
        $this->currency = $currency;
    }
    
    // getters and immutable math methods
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)