德米特定律和返回值
根据Demeter法则,你可以在返回的对象上调用方法吗?
例如
<?php
class O
{
public function m($http)
{
$response = $http->get('http://www.google.com');
return $response->getBody(); // violation?
}
}
?>
$http->get() 返回一个对象。这算不算是在 M 中创建/实例化的对象?如果你不能调用它的方法(根据 LoD),你会如何处理这种情况?
According to the Law of Demeter, can you call methods on returned objects?
E.g.
<?php
class O
{
public function m($http)
{
$response = $http->get('http://www.google.com');
return $response->getBody(); // violation?
}
}
?>
$http->get() returns an object. Does this count as an object created/instantiated within M? If you can not call methods on it (according to LoD), how would you handle this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不违反德米特法则,给出:
由于 $response 是在 M 内创建的对象,因此您可以调用该对象上的方法无违规。但是,访问
getBody()
之外的属性将构成违规:有时您可以说可以通过说“一点”规则来简化法律,这意味着您可以访问一个属性或方法深。
This is not a violation of the Law of Demeter, given:
Since $response is an object that is created within M, you can invoke a method upon that object without violation. However, it would be a violation to access properties beyond
getBody()
:Sometimes you can say that the law can be simplified by saying it's the "one dot" rule, meaning that you can access one property or method deep.
一方面,
$response
似乎是在方法m
中创建的,因此答案似乎是肯定的。另一方面,由于
$http
已传入m
,因此$http->get()
返回的对象为现在由$response
表示的可能是$http
的成员,该成员可能是在进入m
之前创建的。考虑到法律的“只有一个点”(或在本例中为箭头)解释,将函数主体重写为
return $http->get('http://www.google.com') ->getBody();
表明这可能是违规的。将中间成员保存为局部变量似乎是避免单点原则的一种狡猾方法。我无法给出明确的答案。在某种程度上,我认为这取决于您对 $http->get() 为您提供新创建的对象而不是预先存在的成员的信任程度。
On the one hand,
$response
appears to have been created within methodm
, so the answer would appear to be yes.On the other hand, since
$http
has been passed in tom
, the object returned by$http->get()
that is now represented by$response
might be a member of$http
that might have been created prior to entry tom
.Considering the "only one dot" (or, in this case arrow) interpretation of the Law, rewriting the body of your function as
return $http->get('http://www.google.com')->getBody();
suggests that it might be a violation. Saving the intermediate members as local variables seems like a dodgy way to avoid the one-dot principle.I can't give a definitive answer. To some extent, I think it depends on how much you trust the
$http->get()
to give you a newly created object rather than a pre-existing member.解决这个问题的一种可能性是在 m() 中创建对象,并让 http->get() 用信息填充它。
One possibility to solve this is to create the object within m(), and let http->get() fill it with information.