简体   繁体   English

可以将类实现与其接口硬耦合吗?

[英]Is it ok to Hard Couple a class implementation with its interface?

I try to understand others people code and I saw something quite strange in an implementation regarding how to create a builder.我试图理解其他人的代码,我在一个关于如何创建构建器的实现中看到了一些很奇怪的东西。 Is this design good?这个设计好吗?

public interface Car {
 static Builder createBuilder() {
     return new CarImpl.Builder();
 }
 interface Builder {
  /// Setters contract
 }
}

Generally no, your interface should define the behaviour of a class (the signature of functions), for example in 'Car' exposing functions such as drive, stop, refuel, etc.通常不,你的接口应该定义一个类的行为(函数的签名),例如在“汽车”中暴露诸如驱动、停止、加油等功能。

Your interface should not be aware of implementation details.您的接口不应该知道实现细节。 This is the idea behind an interface - it abstracts away the implementation details so they are incapsulated in whomever layer that is responsible for the implementation, which leaves your interface clean, exposing only the what & not the how.这是接口背后的想法 - 它抽象了实现细节,因此它们被封装在负责实现的任何层中,这使您的接口保持干净,只暴露什么而不是如何。

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

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