且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

模块、库和框架之间的区别

更新时间:2023-12-04 11:26:58

所有这三个都提供了功能.

All three of those provide functionality.

但是,有一些重要的区别.

However, there are important differences.

只是相关功能的集合.仅此而已,但也仅此而已.图书馆的定义特征是在控制,调用图书馆.

A library is just a collection of related functionality. Nothing more, but also nothing less. The defining characteristic of a library is that you are in control, you call the library.

框架的定义特征是控制反转.框架调用,而不是相反.(这被称为好莱坞原则:不要打电话给我们,我们会打电话给你.")框架处于控制之中.控制流和数据流由框架管理.

The defining characteristic of a framework is Inversion of Control. The framework calls you, not the other way round. (This is known as the Hollywood Principle: "Don't call us, we'll call you.") The framework is in control. The flow of control and the flow of data is managed by the framework.

你可以这样想:在这两种情况下,你都有一个应用程序,这个应用程序有漏洞,代码被遗漏了,这些漏洞需要补上. 库和库的区别一个框架是

You can think of it this way: in both cases, you have an application, and this application has holes in it, where code has been left out, and these holes need to be filled in. The difference between a library and a framework is

  • 谁编写了应用程序,
  • 什么是洞和
  • 谁来填补漏洞.

对于图书馆,编写应用程序,而你省略了无聊的细节,这些细节由图书馆填充.

With a library, you write the application, and you leave out the boring details, which gets filled in by a library.

对于框架,框架编写者编写应用程序,并省略有趣的细节,填写.

With a framework, the framework writer writes the application, and leaves out the interesting details, which you fill in.

这有时会让人有点困惑,因为框架本身也可能包含无聊的细节,框架作者用库填充,你编写的部分可能包含无聊的细节,你用库填充,以及框架可能会提供一组捆绑的库,这些库要么与框架很好地配合使用,要么经常需要与框架结合使用.例如,在使用 Web 框架编写 Web 应用程序时,您可能会使用 XML 生成器库,而该 XML 库可能已由框架提供,甚至是框架的一个组成部分.

This can be a little confusing at times, because the framework itself might also contain boring details, that the framework author filled in with libraries, the parts that you write might contain boring details, that you fill in with libraries, and the framework might provide a set of bundled libraries that either work well with the framework or that are often needed in conjunction with the framework. For example, you might use an XML generator library when writing a web application using a web framework, and that XML library might have been provided by the framework or even be an integral part of it.

然而,这并不意味着库和框架之间没有区别.区别非常明显:控制反转就是一切.

That doesn't mean, however, that there is no distinction between a library and a framework. The distinction is very clear: Inversion of Control is what it's all about.

模块的定义特征是信息隐藏.一个模块有一个接口,它明确地但抽象地指定了它提供的功能以及它所依赖的功能.(通常称为exportedimported 功能.)这个接口有一个实现(或多个实现,实际上),它来自于一个模块是一个黑盒子.

The defining characteristic of a module is information hiding. A module has an interface, which explicitly, but abstractly specifies both the functionality it provides as well as the functionality it depends on. (Often called the exported and imported functionality.) This interface has an implementation (or multiple implementations, actually), which, from the user of a module are a black box.

此外,库是相关功能的集合,而模块仅提供单个功能.这意味着,如果您有一个同时包含模块和库的系统,一个库通常会包含多个模块.例如,您可能有一个集合库,其中包含一个 List 模块、一个 Set 模块和一个 Map 模块.

Also, a library is a collection of related functionality, whereas a module only provides a single piece of functionality. Which means that, if you have a system with both modules and libraries, a library will typically contain multiple modules. For example, you might have a collections library which contains a List module, a Set module and a Map module.

虽然您当然可以在没有模块系统的情况下编写模块,但理想情况下,您希望您的模块可单独编译(对于该概念甚至有意义的语言和执行环境)、可单独部署,并且您希望模块组合是安全的(即组合模块应该在运行前工作或触发错误,但绝不会导致运行时错误或意外行为).为此,您需要一个模块系统,例如 Racket 的单元、Standard ML 的模块和函子或 Newspeak 的***类.

While you can certainly write modules without a module system, ideally you want your modules to be separately compilable (for languages and execution environments where that notion even makes sense), separately deployable, and you want module composition to be safe (i.e. composing modules should either work or trigger an error before runtime, but never lead to an runtime error or unexpected behavior). For this, you need a module system, like Racket's units, Standard ML's modules and functors or Newspeak's top-level classes.

那么,让我们回顾一下:

So, let's recap:

  • :相关功能的集合
  • 框架:控制反转
  • module:具有显式导出和导入的抽象接口,实现和接口是分开的,可能有多个实现并且实现是隐藏的
  • library: collection of related functionality
  • framework: Inversion of Control
  • module: abstract interface with explicit exports and imports, implementation and interface are separate, there may be multiple implementations and the implementation is hidden