简体   繁体   English

什么是胖接口?

[英]What is a fat Interface?

Ciao, I work in movie industry to simulate and apply studio effects. Ciao,我在电影行业工作,以模拟和应用工作室效果。 May I ask what is a fat interface as I hear someone online around here stating it?请问什么是胖界面,我听到这里有人在网上说它?

Edit: It is here said by Nicol Bolas (very good pointer I believe)编辑:尼科尔波拉斯在这里说(我相信非常好的指针)

fat interface - an interface with more member functions and friends than are logically necessary. fat interface - 一个具有比逻辑上需要的更多成员函数和朋友的接口。 TC++PL 24.4.3 source TC++PL 24.4.3源码

very simple explanation is here :非常简单的解释在这里

The Fat Interface approach [...]: in addition to the core services (that are part of the thin interface) it also offers a rich set of services that satisfy common needs of client code.胖接口方法 [...]:除了核心服务(作为瘦接口的一部分)之外,它还提供了一组丰富的服务来满足客户代码的常见需求。 Clearly, with such classes the amount of client code that needs to be written is smaller.显然,使用此类类,需要编写的客户端代码量更少。

When should we use fat interfaces?我们什么时候应该使用胖接口? If a class is expected to have a long life span or if a class is expected to have many clients it should offer a fat interface.如果 class 预计具有较长的使用寿命,或者如果 class 预计有许多客户端,则应提供胖接口。

Maxim quotes Stroustrup's glossary: Maxim 引用 Stroustrup 的词汇表:

fat interface - an interface with more member functions and friends than are logically necessary. fat interface - 一个具有比逻辑上需要的更多成员函数和朋友的接口。 TC++PL 24.4.3 TC++PL 24.4.3

Maxim provides no explanation, and other existing answers to this question mis interpret the above - or sans the Stroustrup quote the term itself - as meaning an interface with an arguably excessive number of members. Maxim 没有提供任何解释,并且对这个问题的其他现有答案错误地解释了上述内容 - 或者没有 Stroustrup 引用该术语本身 - 意味着具有可以说是过多成员数量的接口。 It's not.它不是。

It's actually not about the number of members , but whether the members make sense for all the implementations .这实际上不是关于成员的数量,而是关于成员是否对所有实现都有意义

That subtle aspect that doesn't come through very clearly in Stroustrup's glossary, but at least in the old version of TC++PL I have - is clear where the term's used in the text.在 Stroustrup 的词汇表中没有很清楚地表达出来的那个微妙的方面,但至少在我拥有的旧版本的 TC++PL 中 - 很清楚该术语在文本中的使用位置。 Once you understand the difference, the glossary entry is clearly consistent with it, but "more member functions and friends than are logically necessary" is a test that should be applied from the perspective of each of the implementations of a logical interface.一旦理解了区别,词汇表条目显然与之一致,但是“比逻辑上需要的成员函数和朋友更多”是应该从逻辑接口的每个实现的角度应用的测试。 (My understanding's also supported by Wikipedia , for whatever that's worth;-o.) (我的理解也得到了 Wikipedia的支持,不管它值多少钱;-o。)

Specifically when you have an interface over several implementations, and some of the interface actions are only meaningful for some of the implementations, then you have a fat interface in which you can ask the active implementation to do something that it has no hope of doing, and you have to complicate the interface with some "not supported" discovery or reporting , which soon adds up to make it harder to write reliable client code .特别是当您有多个实现的接口时,并且某些接口操作仅对某些实现有意义,那么您有一个胖接口,您可以在其中要求活动实现做一些它没有希望做的事情,而且您必须通过一些“不支持”的发现或报告来使界面复杂化,这很快就会使编写可靠的客户端代码变得更加困难

For example, if you have a Shape base class and derived Circle and Square classes, and contemplate adding a double get_radius() const member: you could do so and have it throw or return some sentinel value like NaN or -1 if called on a Square - you'd then have a fat interface.例如,如果你有一个Shape基础 class 和派生的CircleSquare类,并考虑添加一个double get_radius() const成员:你可以这样做并让它throw或返回一些标记值,如NaN-1如果在一个Square - 然后你会有一个胖界面。


"Uncle Bob" puts a different emphasis on it below (boldfacing mine) in the context of the Interface Segregation Principle (ISP) (a SOLID principle that says to avoid fat interfaces): “鲍勃叔叔”接口隔离原则 (ISP) (一种避免胖接口的SOLID原则)的背景下,在下面(粗体字)对它进行了不同的强调:

[ISP] deals with the disadvantages of “fat” interfaces. [ISP] 处理“胖”接口的缺点。 Classes that have “fat” interfaces are classes whose interfaces are not cohesive .具有“胖”接口的类是其接口没有内聚性的类。 In other words, the interfaces of the class can be broken up into groups of member functions.换言之, class 的接口可以分解为成员函数组。 Each group serves a different set of clients.每个组都服务于不同的客户集。 Thus some clients use one group of member functions, and other clients use the other groups .因此,一些客户端使用一组成员函数,而其他客户端使用其他组

This implies you could have eg virtual functions that all derived classes do implementation with non-noop behaviours, but still consider the interface "fat" if typically any given client using that interface would only be interested in one group of its functions.这意味着您可以拥有所有派生类都使用非 noop 行为实现的虚函数,但如果通常使用该接口的任何给定客户端只对它的一组函数感兴趣,则仍然认为接口“胖”。 For example: if a string class provided regexp functions and 95% of client code never used any of those, and especially if the 5% that did didn't tend to use the non-regexp string functions, then you should probably separate the regexp functionality from the normal textual string functionality.例如:如果一个字符串 class 提供了正则表达式函数,并且 95% 的客户端代码从未使用过其中任何一个,尤其是如果 5% 没有使用非正则表达式字符串函数,那么您可能应该将正则表达式分开来自普通文本字符串功能的功能。 In that case though, there's a clear distinction in member function functionality that forms 2 groups, and when you were writing your code you'd have a clear idea whether you wanted regexp functionality or normal text-handling functionality.但是,在这种情况下,forms 2 组的成员 function 功能有明显的区别,并且当您编写代码时,您会清楚地知道您想要正则表达式功能还是普通的文本处理功能。 With the actual std::string class, although it has a lot of functions I'd argue that there's no clear grouping of functions where it would be weird to evolve a need to use some functions (eg begin / end ) after having initially needed only say insert / erase .使用实际的std::string class,虽然它有很多功能,但我认为没有明确的功能分组,在最初需要之后发展需要使用某些功能(例如begin / end )会很奇怪只说insert / erase I don't personally consider the interface "fat", even though it's huge.我个人并不认为界面“胖”,即使它很大。


Of course, such an evocative term will have been picked up by other people to mean whatever they think it should mean, so it's no surprise that the web contains examples of the simpler larger-than-necessary-interface usage, as evidenced by the link in relaxxx's answer, but I suspect that's more people guessing at a meaning than "educated" about prior usage in Computing Science literature....当然,其他人会选择这样一个令人回味的术语来表示他们认为应该表示的任何含义,因此 web 包含更简单的大于必要的接口使用示例也就不足为奇了,正如链接所证明的那样在relaxxx的回答中,但我怀疑更多的人猜测含义,而不是“受过教育”关于计算机科学文献中先前用法的知识......

An interface with more methods or friends than is really necessary.具有比实际需要更多的方法或朋友的接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM