通过查看 Zend 快速入门教程中的域对象示例以及考虑 DAO/VO 模式的其他示例,它们似乎非常相似。
我们能否推断出“值对象”与“域对象”是一样的?
如果不是,您能澄清一下它们之间的区别吗?
其中一个的功能是什么,如果另一个的功能又如何呢?
我问这个是因为,两者都是由 getter 和 setter 组成,仅此而已。看起来,它们执行相同的功能......
Update:
因此,Zend Framework 快速教程文档将其称为域对象:
// application/models/Guestbook.php
class Application_Model_Guestbook
{
protected $_comment;
protected $_created;
protected $_email;
protected $_id;
public function __construct(array $options = null)
{
if (is_array($options)) {
$this->setOptions($options);
}
}
public function __set($name, $value)
{
$method = 'set' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid guestbook property');
}
$this->$method($value);
}
public function __get($name)
{
$method = 'get' . $name;
if (('mapper' == $name) || !method_exists($this, $method)) {
throw new Exception('Invalid guestbook property');
}
return $this->$method();
}
public function setOptions(array $options)
{
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
return $this;
}
public function setComment($text)
{
$this->_comment = (string) $text;
return $this;
}
public function getComment()
{
return $this->_comment;
}
public function setEmail($email)
{
$this->_email = (string) $email;
return $this;
}
public function getEmail()
{
return $this->_email;
}
public function setCreated($ts)
{
$this->_created = $ts;
return $this;
}
public function getCreated()
{
return $this->_created;
}
public function setId($id)
{
$this->_id = (int) $id;
return $this;
}
public function getId()
{
return $this->_id;
}
}
1)严格来说,我们面对的是“贫血领域对象”吗?
2)是否称为“域对象”just因为它包含领域逻辑?
3)如果是这种情况,那么那些包含 findBookByAuthor(); 等方法的映射器他们也在处理领域逻辑,对吧?它们也可以被视为域对象吗?
多谢
通常,值对象封装具有值的内容:货币、日期、温度等。它们可能包含值和单位,但它们并不复杂。
域对象可能更复杂(除非它是贫血域对象,它是一堆假装是域对象的 getter 和 setter),因为它包含域逻辑。
例如,您可能有一个发票域对象,其中包含许多发票行(每个发票项目一行),每个发票行可能有一个净额、一个税额和一个发票项目。金额和发票项目通常是值对象并且相当简单。
发票本身可能会因延迟付款的利率、审批流程的支持或会计系统的支持而变得复杂。
值对象足够简单,可以跨不同域重用。域对象对您的实际域进行建模,通常是为了对您的特定业务或域(包括业务逻辑)进行建模而编写的。
您经常会看到它们之间几乎没有区别的原因是许多开发人员将使用事务脚本/数据传输对象设计,但将其称为域模型。他们将 getter 和 setter 的集合标记为“域对象”。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)