流畅的界面是否违反了迪米特法则? [英] Do fluent interfaces violate the Law of Demeter?

查看:36
本文介绍了流畅的界面是否违反了迪米特法则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于维基百科文章/wiki?LawOfDemeter" rel="noreferrer">德米特法则 说:

The wikipedia article about Law of Demeter says:

法律可以简单地表述为只使用一个点".

The law can be stated simply as "use only one dot".

然而一个 流畅界面的">简单示例可能如下所示:

However a simple example of a fluent interface may look like this:

static void Main(string[] args)
{
   new ZRLabs.Yael.Pipeline("cat.jpg")
        .Rotate(90)
        .Watermark("Monkey")
        .RoundCorners(100, Color.Bisque)
        .Save("test.png");
}

所以这可以一起使用吗?

So does this goes together?

推荐答案

好吧,法律的简短定义将其缩短了太多.真正的法律"(实际上是关于良好 API 设计的建议)基本上是这样说的:只能访问您自己创建的对象,或者作为参数传递给您.不要通过其他对象间接访问对象.流畅接口的方法通常返回对象本身,因此如果您再次使用该对象,它们不会违反法律.其他方法为您创建对象,因此也没有违规.

Well, the short definition of the law shortens it too much. The real "law" (in reality advice on good API design) basically says: Only access objects you created yourself, or were passed to you as an argument. Do not access objects indirectly through other objects. Methods of fluent interfaces often return the object itself, so they don't violate the law, if you use the object again. Other methods create objects for you, so there's no violation either.

另请注意,法律"只是对经典"API 的最佳实践建议.Fluent 接口是一种完全不同的 API 设计方法,无法用迪米特定律进行评估.

Also note that the "law" is only a best practices advice for "classical" APIs. Fluent interfaces are a completely different approach to API design and can't be evaluated with the Law of Demeter.

这篇关于流畅的界面是否违反了迪米特法则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆