界面的要点 [英] The point of an Interface

查看:152
本文介绍了界面的要点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我将如何知道什么时候创建界面?

我想知道使用接口的要点。

I'm wondering about the point of using an Interface.

您是否使用界面?如果是,您什么时候决定使用它们,何时决定不使用它们?

Do you use Interfaces? If so, when do you decide to use them and when do you decide NOT to use them?

我现在已经为我的服务层和我的存储库层定义了接口,但我想知道我是否在其他有用的地方错过了。

I've currently got interfaces defined for my service layers and my repository layers, but I'm wondering if I'm missing out on other places where they'd be useful.

我想我完全不了解他们的目的。 / p>

I guess I just don't fully understand their purpose.

推荐答案

一个接口定义了一个合同。任何实现接口的类必须满足该合同。这意味着该类必须实现接口中定义的方法。

An interfaces defines a contract. Any class that implements an interface has to fulfil that contract. This means that the class must implement methods defined in the interface.

一个接口基本上说我正在定义所有实现者必须做的事情,我不在乎你们这样做,但你必须支持我指定的这些操作。

An interface basically says "I'm defining something that all implementers must do. I don't care how you do it, but you must support these operations that I've specified".

接口的另一个用途是,您可以在方法签名或类型定义中使用它们来指定最通用的类​​型目的。例如,在Java中 Map 是由其他类(如 HashMap LinkedHashMap的 HashMap LinkedHashMap 基本上是类型 Map 。他们实现相同的方法,但它们做的不同( LinkedHashMap 保留插入顺序)。

Another use of interfaces is that you can use them in method signatures or type definitions to specify the most generic type of an object. For example, in Java Map is an interface that is implemented by other classes like HashMap or LinkedHashMap. Both HashMap and LinkedHashMap are essentially of type Map. They implement the same methods, but they do things differently (LinkedHashMap preserves insertion-order).

你有一个接受地图的方法。如果您没有接口,则需要为每种类型的地图指定一种方法。实际上,你可以通过重载的方法来做到这一点,但这种做法不是很好。更好的方法是将方法参数的类型指定为 Map 。然后,任何实现 Map 的类都可以传递给该方法。这样,您不必为每种类型的地图指定一种方法,而且您也不会将使用方法的人限制在地图的具体实施中。

Consider the situation where you have a method that accepts maps. If you didn't have interfaces, you would need to specify a method for each type of map. Indeed, you could do that via overloaded methods, but that approach is not very good. The better way is to specify the type of the method argument as Map. Then, any class that implements Map can be passed in to the method. This way, you don't have to specify a method for each type of map, and also you are not restricting the person who uses your method, to specific implementations of the map.

接口还保证在实现类中存在指定的功能。因此,它还提供了访问该功能的标准方法。当您设计API时,界面也很有用(这样可以指定要暴露的内容的标准界面)。

An interface also guarantees that the specified functionality is present in implementing classes. As such, it also provides a standard way to access that functionality. Interfaces are also useful when you are designing an API (that way you can specify a standard interface to the things you want to expose).

接口的另一个好处是它使得重构变得容易。假设您想要切换某种对象的实现。该对象可能是一个方法参数,也可以是一个类属性。由于您键入该参数或对象作为接口,您可以简单地创建一个实现该接口并传递该类的新类。由于您使用该界面,您没有对该类的详细信息做出额外的假设。该界面摘录了您正在使用的类的实现细节。这样你就不会做出假设,使你的代码太紧密地耦合到一个具体的实现。

Another benefit of interfaces is that it makes refactoring easy. Let's say that you want to switch out an implementation of a some sort of object. The object might be a method argument or it may be a class property. Since you've typed that argument or object as an interface, you can simply create a new class that implements the interface and pass that class instead. Since you used the interface, you didn't make an extra assumptions as to the details of the class. The interface abstracts out the implementation details of the class that you're using. That way you don't end up making assumptions that makes your code too tightly-coupled to a specific implementation.

总结一下,接口是关于抽象合约。使用抽象可以隐藏底层的细节,仅暴露出您需要暴露的最低限度。这样,使用你的类或接口的人不负责实施细节。所有这些信息整齐地隐藏在实现该接口的特定类中。 合同确保全面实现标准化;使用该界面的人员确保实现该界面的所有类都会显示相同的方法。

To sum it up, interfaces are about abstraction and contracts. With abstraction you hide the underlying details and expose only the bare minimum that you need to expose. That way the person who uses your class or interface is not burdened with the implementation details. All that information is neatly hidden inside the specific class that implements the interface. The contract ensures standardization across the board; a person who uses the interface is sure that all classes that implement the interface expose the same methods.

这篇关于界面的要点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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