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

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


当前回答

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

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

其他回答

实际上,根据使用的上下文,这些术语可以有很多不同的含义。

For example, on Mac OS X frameworks are just libraries, packed into a bundle. Within the bundle you will find an actual dynamic library (libWhatever.dylib). The difference between a bare library and the framework on Mac is that a framework can contain multiple different versions of the library. It can contain extra resources (images, localized strings, XML data files, UI objects, etc.) and unless the framework is released to public, it usually contains the necessary .h files you need to use the library.

Thus you have everything within a single package you need to use the library in your application (a C/C++/Objective-C library without .h files is pretty useless, unless you write them yourself according to some library documentation), instead of a bunch of files to move around (a Mac bundle is just a directory on the Unix level, but the UI treats it like a single file, pretty much like you have JAR files in Java and when you click it, you usually don't see what's inside, unless you explicitly select to show the content).

维基百科称框架为“流行词”。它将软件框架定义为

软件框架是可重用的 为软件系统(或 子系统)。软件框架可以 包括支持程序、代码 库、脚本语言或 其他软件帮助开发和 把不同的组件粘合在一起 软件项目的。各个部分 框架的部分可能是公开的 通过一个API..

所以我认为图书馆就是“一个图书馆”。它是对象/函数/方法的集合(取决于你的语言),你的应用程序“链接”到它,因此可以使用对象/函数/方法。它基本上是一个包含可重用代码的文件,通常可以在多个应用程序之间共享(您不必一遍又一遍地编写相同的代码)。

框架可以是您在应用程序开发中使用的所有内容。它可以是一个库,许多库的集合,脚本的集合,或者创建应用程序所需的任何软件。框架是一个非常模糊的术语。

Here's an article about some guy regarding the topic "Library vs. Framework". I personally think this article is highly arguable. It's not wrong what he's saying there, however, he's just picking out one of the multiple definitions of framework and compares that to the classic definition of library. E.g. he says you need a framework for sub-classing. Really? I can have an object defined in a library, I can link against it, and sub-class it in my code. I don't see how I need a "framework" for that. In some way he rather explains how the term framework is used nowadays. It's just a hyped word, as I said before. Some companies release just a normal library (in any sense of a classical library) and call it a "framework" because it sounds more fancy.

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

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

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

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

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

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

什么是图书馆?

库是代码块的集合(可以是变量、函数、类、接口等形式),由开发人员构建,以简化其他发现其相关性的开发人员的软件开发过程。

什么是框架?

参考库的定义,我们可以将框架定义为一种工具,通过在受控的开发环境中为开发人员提供必要的库,帮助开发人员解决大量特定于领域的问题。

库vs框架

Martin Fowler - InversionOfControl

库和框架是你的代码的外部代码。它可以是文件(例如。jar),系统代码(操作系统的一部分)等。

库是一组有用的代码。主要关注你的代码。库解决的任务范围很窄。例如,实用程序,排序,模块化

your code ->(has) Library API

框架或控制反转(IoC)容器[About]是更多的东西。框架解决了广泛的任务(特定于领域),你委派这个任务给框架。IoC——你的代码依赖于框架逻辑、事件…因此框架会调用你的代码。它迫使你的代码坚持它的规则(实现/扩展协议/接口/契约),传递lambdas…例如-测试,GUI, DI框架…

your code ->(has) and ->(implements) Framework API

[iOS Library vs Framework]

[DIP vs DI vs IoC]