通过查看 Zend 快速入门教程中的域对象示例,以及考虑 DAO/VO 模式的其他示例,它们似乎都非常相似。
我们可以推断说“值对象”与说“域对象”是一样的吗?
如果没有,您能否澄清它们之间的区别?
一个的功能是什么,如果另一个的功能是什么?
我问这个是因为,两者都是由 getter 和 setter 组成的,仅此而已。看来,他们做同样的功能......
更新:
因此,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)是否仅仅因为它包含域逻辑而被称为“域对象” ?
3)如果是这种情况,那么,那些包含 findBookByAuthor() 等方法的映射器;他们也在处理域逻辑,对吗?它们也可以被视为域对象吗?
非常感谢