Scala 和接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/674164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scala and interfaces
提问by oxbow_lakes
In JavaI would typically declare my entire domain as interfaces, possibly with some kind of Factoryto get me implementations. This is partly because I am so old I can remember when some persistence layers required implementation classes to subclass a specific class but also so that I can easily:
在Java我通常会声明我的整个域为interfaces,可能有某种Factory让我实现。这部分是因为我太老了,我记得当某些持久层需要实现类来继承特定类时,我还可以轻松地:
- mock objects for testing purposes
- proxy objects at runtime if necessary
- provide different implementations
- 用于测试目的的模拟对象
- 如有必要,在运行时代理对象
- 提供不同的实现
I follow this practice even when I don't really envisage that I'll actually want to do any of the above; I still believe it to be good practice just in case. I think it's a fairly common approach.
即使我真的没想到我真的想做上述任何事情,我也会遵循这种做法;我仍然相信这是一个很好的做法,以防万一。我认为这是一种相当普遍的方法。
Would this be unusual in Scala-land if I declare all domain objects abstract? Do the points above hold for Scala as well?
如果我声明所有域对象,这在 Scala 领域会不寻常abstract吗?以上几点也适用于 Scala 吗?
采纳答案by Saem
Scala has traits, which are interfaces on crack. Really, they're what interfaces should have been, admittedly, there are limitations when it comes to constructors, but that's not really a big deal considering the fact that if you have two interfaces both having requirements of the constructor you'd run into the same issues.
Scala 有特性,它们是破解的接口。确实,它们应该是接口,无可否认,在构造函数方面存在限制,但考虑到如果您有两个接口都具有构造函数的要求,那么这并不是什么大问题,您会遇到同样的问题。
Then there are partial methods, and in a lot of ways many of the good object oriented design principles based around dependency management could almost be seen as ways of getting units of work that are more composable, you really have to wonder. Instead of only being able to work on method inputs and outputs, or predefined points where a strategy object/method is employed you have a bit more flexibility.
然后是部分方法,在很多方面,许多基于依赖管理的良好的面向对象设计原则几乎可以被视为获得更可组合的工作单元的方法,你真的不得不怀疑。不仅能够处理方法输入和输出,或者使用策略对象/方法的预定义点,您还有更多的灵活性。
Add to that companion objects and all of a sudden factories and a whole lot more become far more trivial.
添加到那些伴随对象和突然之间的工厂等等变得更加微不足道。
Given that's the case, you really can get away from having to use interfaces everywhere, and with more powerful generics system, some of what gets done with interfaces gets sucked up there.
鉴于这种情况,您真的可以摆脱在任何地方都必须使用接口的情况,并且有了更强大的泛型系统,使用接口完成的一些工作就会被困在那里。
Generally looking at Scala code it tends to be rather decomposed, and interfaces don't seem to be the main tool in that.
通常查看 Scala 代码,它往往被分解,接口似乎不是其中的主要工具。
回答by MarkusQ
That's a really thought-provoking question.
这真是一个发人深省的问题。
It doesn't seem to be a common pattern (at least in the Scala I've seen), but I can't off the top of my head think of a good argument against it if that's what you really want to do.
这似乎不是一个常见的模式(至少在我见过的 Scala 中),但如果那是你真正想要做的,我无法想出一个反对它的好论据。
On the other hand, I don't think I'ddo it that way (I'd refactor when I had an actual need, rather than building in such "flexibility" for a hypothetical future that might never come). But the best argument against it I can think of is the analogy to avoiding needless indirection (don't use a pointer to a pointer to a pointer to an integer when all you need is an integer), and that's not very compelling.
另一方面,我不认为我会那样做(当我有实际需要时我会重构,而不是为一个可能永远不会到来的假设未来建立这种“灵活性”)。但我能想到的最好的反对理由是避免不必要的间接引用(当你只需要一个整数时,不要使用指向指向整数的指针的指针)的类比,这不是很有说服力。

