什么时候应该使用接口? [英] When should one use interfaces?

查看:787
本文介绍了什么时候应该使用接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道界面没有正文只是方法定义。但什么时候应该使用接口?如果我向某人提供一组没有正文的接口,为什么他们觉得需要编写函数体?他们最好用抽象方法编写自己的抽象类。

I know that an interface does not have a body just a method definition. But when should I use interfaces? If I provide someone a set of interfaces with no body, why would they feel a need to write the function body? Would they be better off writing their own abstract class with abstract methods in it.

我猜当您是团队的一员时,使用Interfaces更多。假设A队为某事写了一个代码,他们想看看是否调用了一个方法。使用名称getRecords(),是否完成。这将帮助B队编写提供给他们的界面的主体,B队必须保持方法名称相似,以便A队的代码运行。

I guess the use of Interfaces is more when you are a part of a team. Suppose Team A writes a code for something and they wanted to see if a call to a method. with name getRecords(), is done or not. This will help Team B to write the body of the interface provided to them and Team B has to keep the method name similar so that code of Team A runs.

只是一个思想。我可能错了。我认为接口对单个开发人员没用。

Just a thought. I might be wrong. I think Interfaces have no use for a single developer.

全部谢谢你的回答。根据你们所回复的内容,我认为当你制作像API这样的东西时,接口有更多的用途吗?

Thanks all for the answers. With what you all have replied, I think Interfaces have more use when you are making something like API?

推荐答案

在诸如Java和C#接口为多态方式提供了一种类。也就是说一个类可以满足多个契约 - 它可以表现为多个不同的类型,一个类的类可以替代另一个类。在其他语言中,这也可以通过多重继承来提供,但这种方法存在各种缺点。但是,让一个类表现为多个类型并不是使用接口的最常见动机。

In languages such as Java and C# interfaces provide a means for a class to be have in a polymorphic manner. That is to say a class can satisfy more than one contract - it can behave as multiple different types, a class of one type can be substituted for another. In other languages this can also be provided by multiple inheritance, but there are various disadvantages to this approach. However, having a class behave as more than one type is not the most common motivation for using interfaces.

通过编程到接口而不是类,您还可以将程序与具体实施。这使得将一个类实现替换为另一个类更容易。这在编写单元测试时特别有用,你可能希望用轻量级的模拟对象交换一些重量级的类实现。如果您的程序只需要一个接口类型,并且重量级对象和模拟对象都实现了所述接口,那么它们很容易替换。

By programming to interfaces instead of classes you can also decouple your program from specific implementations. This makes it much easier to substitute one class implementation for another. This is particularly useful when writing unit tests where you may wish to swap some heavyweight class implementation with a lightweight mock object. If your program only expects an interface type, and both the heavyweight object and mock object implement said interface, then they are very easy to substitute.

另外,考虑一个简单的Java例如,我说有一个程序在屏幕上显示数据页面。最初我希望它从数据库或XML文件中获取数据。如果我编写程序以便它使用接口,我可以定义一个接口,如下所示:

Also, consider a simple Java example where I say have a program that displays pages of data on the screen. Initially I want it to get the data from a database or XML files. If I write my program so that it uses interfaces I can define an interface like so:

public interface PageDatasource {
    public List<Page> getPages();
}

使用它如下:

PageDatasource datasource = // insert concrete PageDatasource implementation here
List<Pages> pages = datasource.getPages();
display(pages);

然后,我可以编写符合此接口的单独数据库和XML实现:

I can then write separate database and XML implementations that adhere to this interface:

public class DatabasePageDatasource implements PageDatasource {
    public List<Page> getPages() {
        // Database specific code
    }
}

public class XmlPageDatasource implements PageDatasource {
    public List<Page> getPages() {
        // XML specific code
    }
}

因为我使用了一个接口,我现在可以使用任一实现 - 数据库或XML - 可以互换,而不必更改我的程序中要求页面数据的部分。 XML和数据库实现可能完全不同,但我的程序关心的是提供页面数据的对象实现 PageDatasource 接口。

Because I used an interface I can now use either implementation - Database or XML - interchangeably without having to change the parts of my program that ask for the page data. The XML and Database implementations likely do completely different things but all my program cares about is that the object supplying the page data implements the PageDatasource interface.

这篇关于什么时候应该使用接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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