如果你创建时(目前)只将是一个类实现它的接口? [英] Should you create an interface when there (currently) is only going to be one class that implements it?

查看:153
本文介绍了如果你创建时(目前)只将是一个类实现它的接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你总是创建一个接口,如果有可能,有可能是别的东西,可以使用它,或者等到有实际需要它,然后重构为使用接口?

Should you always create an interface if there's a possibility that there might be something else that could use it, or wait until there's an actual need for it then refactor to use an interface?

面向接口编程通常似乎是合理的建议,但再有就是YAGNI ...

Programming to an interface generally seems like sound advice, but then there's YAGNI...

我想也许这取决于具体情况。现在我有一个代表,可以包含配方或其他文件夹的文件夹的对象。而不是直接使用文件夹,我应该担心实施类似的IContainer?如果将来我希望能有一个指向其它子食谱配方(比如一个苹果派食谱,也是一个馅饼皮配方的容器)

I guess maybe it depends on the situation. Right now I have an object representing a folder that can contain recipes or other folders. Instead of using Folder directly, should I worry about implementing something like IContainer? In case in the future I want to have a recipe that refers to other sub recipes (say an apple pie recipe that is also a container for a pie crust recipe)

推荐答案

它总是取决于具体情况。如果你知道有将要使用的接口另一个类,然后是,创建接口类后,以节省时间。但是,如果你不知道(而且大部分你是不是时间),然后等到你需要它。

It always depends on the situation. If you KNOW there is going to be another class using the interface, then yes, create the interface class to save time later. However, if you are not sure (and most of the time you aren't) then wait till you need it.

现在,这并不意味着忽视的可能性接口 - 想想对象的公共方法和使用等朝后作出接口的眼球,但不与任何你实际上不需要弄乱你的代码

Now that doesn't mean to ignore the possibility of the interface - think about the object's public methods and such with an eye toward making an interface later, but don't clutter your codebase with anything that you don't actually NEED.

这篇关于如果你创建时(目前)只将是一个类实现它的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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