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

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


当前回答

根据Erich Gamma等人在《设计模式》一书中给出的定义:

库:构成可重用实现的一组相关过程和类; 框架:由一组具有模板方法的协作类组成的可重用规范。它设置了控制流,并允许通过在子类中重写框架类中模板方法调用的钩子方法来将框架裁剪到特定问题的流中。

特定于问题的代码可以使用库和实现框架。

其他回答

我认为主要的区别是框架遵循“好莱坞原则”,即。“别打给我们,我们会打给你的。”

马丁·福勒说:

库本质上是一组 你可以调用的函数 日子通常安排在课堂上。 每个调用都做一些工作并返回 控件到客户端。 框架体现了一些抽象 设计,内置了更多的行为。 为了使用它,你需要插入 你的行为变成了各种各样的地方 框架可以通过子类化或 通过插入你自己的类。的 框架的代码然后调用你的代码 在这些点上。

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

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

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

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

图书馆是为了方便使用和提高效率。例如,您可以说Zend库通过其定义良好的类和函数帮助我们完成不同的任务。而框架通常强制以某种方式实现解决方案,如MVC(模型-视图-控制器)(引用)。它是一个定义良好的系统,用于像MVC一样分配任务。模型包含数据库端,视图用于UI接口,控制器用于业务逻辑。

库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]

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

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.