弃用 API 和旧 API 之间的区别? [英] Difference between a deprecated and a legacy API?
问题描述
我正在研究 Java 的 Collection Framework
中的遗留 API,我了解到诸如 Vector
和 HashTable
之类的类已被 ArrayList
和 HashMap
.
I was studying the legacy API's in the Java's Collection Framework
and I learnt that classes such as Vector
and HashTable
have been superseded by ArrayList
and HashMap
.
然而,它们仍然没有被弃用,并且在本质上被视为遗留,弃用适用于被取代且应避免的软件功能,因此,我不确定 API 何时被视为遗留以及何时被弃用.
However still they are NOT deprecated, and deemed as legacy when essentially, deprecation is applied to software features that are superseded and should be avoided, so, I am not sure when is a API deemed legacy and when it is deprecated.
推荐答案
来自 Sun 官方词汇表:
From the official Sun glossary:
弃用:指不再推荐的类、接口、构造函数、方法或字段,并且可能在未来版本中不再存在.
deprecation: Refers to a class, interface, constructor, method or field that is no longer recommended, and may cease to exist in a future version.
来自弃用指南的方法和时间:
From the how-and-when to deprecate guide:
您可能听说过自嘲式幽默"这个词.或使演讲者重要性最小化的幽默.不推荐使用的类或方法就是这样.它不再重要.事实上,您不应再使用它是如此不重要,因为它已被取代并且将来可能不复存在.
You may have heard the term, "self-deprecating humor," or humor that minimizes the speaker's importance. A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that you should no longer use it, since it has been superseded and may cease to exist in the future.
@Deprecated
注释更进一步并警告危险:
The @Deprecated
annotation went a step further and warn of danger:
注释了 @Deprecated
的程序元素是不鼓励程序员使用的,通常是因为它危险,或者因为存在更好的替代方案.
A program element annotated
@Deprecated
is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
参考资料
- java.sun.com 词汇表
- 语言指南/如何以及何时弃用API
- 不推荐使用的注释类型 API莉>
请注意,官方词汇表并没有定义什么是遗留".方法.很有可能,这可能是乔什·布洛赫 (Josh Bloch) 在没有确切定义的情况下使用的术语.不过,这意味着永远不应该在新代码中使用遗留类,并且存在更好的替代品.
Note that the official glossary does not define what "legacy" means. In all likelihood, it may be a term that Josh Bloch used without an exact definition. The implication, though, is always that a legacy class should never be used in new code, and better replacement exists.
也许使用遗留但未弃用类的旧代码不需要任何操作,因为至少现在,它们不会在未来版本中停止存在.
Perhaps an old code using legacy but non-deprecated class requires no action, since for now at least, they aren't in danger of ceasing to exist in future version.
相反,弃用明确警告它们可能不复存在,因此应采取措施迁移到替代品.
In contrast, deprecation explicitly warns that they may cease to exist, so action should be taken to migrate to the replacement.
为了比较这些术语在上下文中的使用方式,这些是书中出现的词已弃用":
For comparison on how these terms are used in context, these are quotes from the book where the word "deprecated" appears:
第 7 条:避免终结器:声称可以保证终结的唯一方法是
System.runFinalizersOnExit
及其邪恶的孪生子Runtime.runFinalizersOnExit
.这些方法存在致命缺陷,已被弃用.Item 7: Avoid finalizers: The only methods that claim to guarantee finalization are
System.runFinalizersOnExit
and its evil twinRuntime.runFinalizersOnExit
. These methods are fatally flawed and have been deprecated.条款 66:同步访问共享可变数据:库提供了
Thread.stop
方法,但是这个方法很久以前就被弃用了,因为它本质上不安全 -- 它的使用会导致数据损坏.Item 66: Synchronize access to shared mutable data: The libraries provide the
Thread.stop
method, but this method was deprecated long ago because it's inherently unsafe -- its use can result in data corruption.项目 70:文档线程安全:
System.runFinalizersOnExit
方法是线程敌对的,已被弃用.Item 70: Document thread safety: The
System.runFinalizersOnExit
method is thread-hostile and has been deprecated.条款 73:避免线程组:它们允许您一次将某些
Thread
原语应用于一堆线程.其中一些原语已被弃用,其余的很少使用.[...] 线程组已过时.Item 73: Avoid thread groups: They allow you to apply certain
Thread
primitives to a bunch of threads at once. Several of these primitives have been deprecated, and the remainder are infrequently used. [...] thread groups are obsolete.相比之下,这些是出现legacy"这个词的引号:
By contrast, these are the quotes where the word "legacy" appears:
条款 23:不要在新代码中使用原始类型:提供它们是为了与引入泛型之前的遗留代码兼容和互操作.
Item 23: Don't use raw types in new code: They are provided for compatibility and interoperability with legacy code that predates the introduction of generics.
条款 25:比数组更喜欢列表:擦除是允许泛型类型与不使用泛型的遗留代码自由互操作的原因.
Item 25: Prefer lists to arrays: Erasure is what allows generic types to interoperate freely with legacy code that does not use generics.
条款 29:考虑类型安全的异构容器:这些包装器可用于跟踪谁将错误类型的元素添加到混合了通用代码和遗留代码的应用程序中的集合中.
Item 29: Consider typesafe heterogeneous containers: These wrappers are useful for tracking down who adds an incorrectly typed element to a collection in an application that mixes generic and legacy code.
第 54 项:明智地使用本机方法:它们提供对遗留代码库的访问,这反过来又可以提供对遗留数据的访问.[...] 使用本机方法访问遗留代码也是合法的.[...] 如果您必须使用本机方法来访问低级资源或遗留库,请尽可能少使用本机代码并对其进行彻底测试.
Item 54: Use native methods judiciously: They provide access to libraries of legacy code, which could in turn provide access to legacy data. [...] It is also legitimate to use native methods to access legacy code. [...] If you must use native methods to access low-level resources or legacy libraries, use as little native code as possible and test it thoroughly.
条款 69:优先使用并发实用程序来等待和通知:虽然您应该始终优先使用并发实用程序而不是
wait
和notify
,您可能需要维护使用wait
和notify
的遗留代码.Item 69: Prefer concurrency utilities to wait and notify: While you should always use the concurrency utilities in preference to
wait
andnotify
, you might have to maintain legacy code that useswait
andnotify
.这些引述不是精心挑选的:它们都是书中出现已弃用" 和遗留" 一词的例子.Bloch 的信息在这里很清楚:
These quotes were not carefully selected: they're ALL instances where the word "deprecated" and "legacy" appear in the book. Bloch's message is clear here:
- 已弃用的方法,例如
Thread.stop
,是危险的,绝对不应该使用. - 另一方面,例如
wait/notify
可以保留在遗留代码中,但不应在新代码中使用.
- Deprecated methods, e.g.
Thread.stop
, are dangerous, and should never be used at all. - On the other hand, e.g.
wait/notify
can stay in legacy code, but should not be used in new code.
我的解释是,贬低某物就是承认它是一个错误,而且一开始就不好.另一方面,将某物归类为遗产就是承认它在过去已经足够好,但它已经达到了目的,不再适合现在和未来.
My interpretation is that deprecating something is admitting that it is a mistake, and was never good to begin with. On the other hand, classifying that something is a legacy is admitting that it was good enough in the past, but it has served its purpose and is no longer good enough for the present and the future.
这篇关于弃用 API 和旧 API 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 已弃用的方法,例如