流利的接口是否违反Demeter定律? [英] Do fluent interfaces violate the Law of Demeter?

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

问题描述

关于维基百科文章 /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");
}

那么这一起去吗?

推荐答案

法律的简短定义将其缩短得太多.真正的法律"(实际上是关于良好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的最佳做法建议.流利的接口是API设计的完全不同的方法,无法用Demeter定律进行评估.

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.

这篇关于流利的接口是否违反Demeter定律?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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