框架和库之间的区别是什么?

我一直认为库是一组对象和函数,专注于解决特定的问题或应用程序开发的特定领域(即数据库访问);另一方面,框架是一个以特定方法论(即MVC)为中心的库的集合,它涵盖了应用程序开发的所有领域。


当前回答

我忘记在哪里看到这个定义了,但我觉得它很好。

库是从代码中调用的模块,框架是调用代码的模块。

其他回答

我喜欢科恩的回答,但更专业的定义是:你的代码调用一个库。框架调用您的代码。例如,GUI框架通过事件处理程序调用代码。web框架通过请求-响应模型调用你的代码。

这也被称为控制反转——突然之间,框架决定何时以及如何执行你的代码,而不是像库那样反过来执行。这意味着框架对代码的结构也有更大的影响。

我忘记在哪里看到这个定义了,但我觉得它很好。

库是从代码中调用的模块,框架是调用代码的模块。

库执行特定的、定义良好的操作。

框架是一个骨架,应用程序通过填充骨架来定义操作的“肉”。框架仍然有代码来连接各个部分,但最重要的工作是由应用程序完成的。

库的例子:网络协议、压缩、图像处理、字符串实用程序、正则表达式计算、数学。操作是独立的。

框架示例:Web应用程序系统,插件管理器,GUI系统。框架定义概念,而应用程序定义最终用户关心的基本功能。

一个框架可以由不同的库组成。让我们举个例子。

Let's say you want to cook a fish curry. Then you need ingredients like oil, spices and other utilities. You also need fish which is your base to prepare your dish on (This is data of your application). all ingredients together called a framework. Now you gonna use them one by one or in combination to make your fish curry which is your final product. Compare that with a web framework which is made out of underscore.js, bootstrap.css, bootstrap.js, fontawesome, AngularJS etc. For an example, Twitter Bootstrap v.35.

Now, if you consider only one ingredient, like say oil. You can't use any oil you want because then it will ruin your fish (data). You can only use Olive Oil. Compare that with underscore.js. Now what brand of oil you want to use is up to you. Some dish was made with American Olive Oil (underscore.js) or Indian Olive Oil (lodash.js). This will only change the taste of your application. Since they serve almost the same purpose, their use depends on the developer's preference and they are easily replaceable.


框架:为应用程序提供独特属性和行为的库的集合。(所有材料) 库:一组定义良好的指令,为数据提供独特的属性和行为。(鱼身上的油) 插件:一个库(ui-router -> AngularJS)或多个库的组合(date-picker -> bootstrap.css + jQuery)的工具构建,如果没有它,你的插件现在可能会正常工作。


P.S. AngularJS是一个MVC框架,但却是一个JavaScript库。因为我相信Library扩展了本地技术(在这里是JavaScript)的默认行为。

你的解释听起来不错。库可以是任何经过编译和自包含的东西,以便在其他代码中重用,实际上对其内容没有任何限制。

另一方面,框架应该具有一系列用于应用程序开发的特定领域的功能,就像您的示例MVC一样。