【发布时间】:2011-08-17 05:26:32
【问题描述】:
通过查看 Zend 快速入门教程中的域对象示例,以及考虑 DAO/VO 模式的其他示例,它们似乎都非常相似。
我们可以推断说“值对象”与说“域对象”是一样的吗?
如果不是,您能澄清一下它们之间的区别吗?
一个的功能是什么,如果另一个的功能呢?
我问这个是因为,两者都是由 getter 和 setter 组成的,仅此而已。看来,它们的功能是一样的……
更新:
所以,Zend Framework Quick Tutorial 文档称之为域对象:
// 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(); 之类的方法;他们也在处理域逻辑,对吗?它们也可以被视为领域对象吗?
非常感谢
【问题讨论】:
标签: oop zend-framework design-patterns value-objects domain-object