MEM*_*MEM 7 oop design-patterns zend-framework domain-object value-objects
通过查看Zend Quickstart教程中的域对象示例以及考虑DAO/VO模式的其他示例,它们似乎都非常相似.
我们可以推断说"价值对象"与"域对象"是一样的吗?
如果没有,你能澄清一下它们之间的区别吗?
一个人的功能是什么,如果另一个人的功能怎么办?
我问这个是因为,两者都是由吸气剂和制定者组成的,仅此而已.看起来,他们做同样的功能......
更新:
所以,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;
}
}
Run Code Online (Sandbox Code Playgroud)
1)严格来说,我们是否面对"贫血领域对象"?
2)它是否仅仅因为它包含域逻辑而被称为"域对象" ?
3)如果是这种情况,那么那些包含像findBookByAuthor()等方法的映射器; 他们还在处理域逻辑吗?他们也可以被视为域对象吗?
非常感谢
Ter*_*cox 11
通常,值对象封装具有值的东西:货币,日期,温度等.它们可能包含值和单位,但它们并不复杂.
域对象可能更复杂(除非它是一个Anemic Domain Object,它是一群假装是域对象的getter和setter),因为它包含域逻辑.
例如,您可能有一个包含许多发票行的发票域对象(每个发票项目的一行),并且每个发票行可能具有净金额,税额和发票项目.金额和可能的发票项目通常是价值对象,并且相当简单.
发票本身可能会因延迟付款的利率,支持审批流程或支持您的会计系统而变得复杂.
Value Object非常简单,可以在不同的域中重用.域对象为您的实际域建模,通常用于为您的特定业务或域建模,包括业务逻辑.
您经常会发现它们之间差别不大的原因是许多开发人员将使用事务脚本/数据传输对象设计,但将其称为域模型.他们将他们的getter和setter集合标记为"域对象".