ChatGPT解决这个技术问题 Extra ChatGPT

What is the difference between a framework and a library? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 months ago. Improve this question

What is the difference between a framework and a library?

I always thought of a library as a set of objects and functions that focuses on solving a particular problem or a specific area of application development (i.e. database access); and a framework on the other hand as a collection of libraries centered on a particular methodology (i.e. MVC) and which covers all areas of application development.

We need some functionality, we call Library. We need some functionality we will take help of framework and Framework calls our code (Eg UIKit).
A software framework is not just a library. It also includes a particular design architecture which users must work within - a sort way in which different items in the library are connected together to provide a generic solution for a particular application type. Frameworks thus have the advantage of needing only configuration by the user and validation of pre-written tests to become operational. With libraries alone users need to design an architecture and create tests before implementation. So more work is needed with just the libraries. But libs are flexible & potentially more efficient.

J
Jason Cohen

A library performs specific, well-defined operations.

A framework is a skeleton where the application defines the "meat" of the operation by filling out the skeleton. The skeleton still has code to link up the parts but the most important work is done by the application.

Examples of libraries: Network protocols, compression, image manipulation, string utilities, regular expression evaluation, math. Operations are self-contained.

Examples of frameworks: Web application system, Plug-in manager, GUI system. The framework defines the concept but the application defines the fundamental functionality that end-users care about.


+1 for real examples. Btw, do you mind clarifying what do you mean by a "web application system"?
@Pacerier probably something that lets you create a full web application, like Rails for Ruby or Sails for Node.js.
Nice answer. Can you give some more examples related to iOS frameworks and libraries ?
or Django, the web framework for perfectionists with deadlines.
I would think of filling out the skeleton with 'muscles' rather than 'meat'. I think it's more accurate analogy because 'muscles' are setting skeleton in motion.
T
Taryn

Actually these terms can mean a lot of different things depending the context they are used.

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).

Wikipedia calls framework a "buzzword". It defines a software framework as

A software framework is a re-usable design for a software system (or subsystem). A software framework may include support programs, code libraries, a scripting language, or other software to help develop and glue together the different components of a software project. Various parts of the framework may be exposed through an API..

So I'd say a library is just that, "a library". It is a collection of objects/functions/methods (depending on your language) and your application "links" against it and thus can use the objects/functions/methods. It is basically a file containing re-usable code that can usually be shared among multiple applications (you don't have to write the same code over and over again).

A framework can be everything you use in application development. It can be a library, a collection of many libraries, a collection of scripts, or any piece of software you need to create your application. Framework is just a very vague term.

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.


Interesting that back in 2008 Wikipedia's article described "framework" as a "buzzword".
IMO, a framework is the "blank" project environement.
I look at the framework as a skeleton or a frame with multiple empty slots and a framework defines what that slots should contain. So, the framework will use all those filled slots and create software. I feel it's kind of a plugin architecture where you plug certain things at certain places and forgot about making them work together. The framework takes care of that. Simple examples of such would be Processing(Creative Art), Spark(General purpose cluster computing), etc.
How is this answer still at the top, when it merely says the question is invalid. Some want to assign different definitions to these different words, and have, so lets talk about that.
@TimotheeHowland My answer is on top as it is the accepted answer. The questioner decides which answer is the accepted one, it is his question after all. And SO is not a discussion site. Also you are free to assign whatever meanings you like to words but then you are inventing your own language. Officially a library describes a collection of software routines and the origin of that term is well known, whereas a framework has no official definition and it is not even known where this term came from.
P
Panos

I think that the main difference is that frameworks follow the "Hollywood principle", i.e. "don't call us, we'll call you."

According to Martin Fowler:

A library is essentially a set of functions that you can call, these days usually organized into classes. Each call does some work and returns control to the client. A framework embodies some abstract design, with more behavior built in. In order to use it you need to insert your behavior into various places in the framework either by subclassing or by plugging in your own classes. The framework's code then calls your code at these points.


@Panos Thanks for the explanation, but can you elaborate. For instance, I am using the FacebookSDK framework, and I am calling class methods from this framework. The FacebookSDK framework is not calling anything in my code, which is the opposite to your definition ie "don't call us, we'll call you."
@CharlesRobertson AFAIK FacebookSDK is a client library. It can not be classified as a framework and the reason is obvious (as you already noticed): The FacebookSDK is not calling anything in client code. Moreover Facebook defines the SDK as follows: "A rich set of client-side functionality for adding Social Plugins, Facebook Login and Graph API calls." No indication for framework...
@Panos Thanks. I guess the suitcase icon that represents the FacebookSDK is a little misleading then. And the name 'FacebookSDK.framework'. Facebook should rename it to something like 'FacebookSDK.dylib'? But thanks for clarifying this. It is good to know what the correct definition is...
That's an interesting definition. I have recently started using d3.js, and observed that it is generally considered as a framework. But whatever d3 code I wrote is within a usual javascript code, so I am unable to extend this definition to d3.
@Dileep The d3.js homepage states in the first sentence: "D3.js is a JavaScript library for manipulating documents based on data". I think that it is wrong to be considered as a framework.
C
Community

Library:

It is just a collection of routines (functional programming) or class definitions(object oriented programming). The reason behind is simply code reuse, i.e. get the code that has already been written by other developers. The classes or routines normally define specific operations in a domain specific area. For example, there are some libraries of mathematics which can let developer just call the function without redo the implementation of how an algorithm works.

Framework:

In framework, all the control flow is already there, and there are a bunch of predefined white spots that we should fill out with our code. A framework is normally more complex. It defines a skeleton where the application defines its own features to fill out the skeleton. In this way, your code will be called by the framework when appropriately. The benefit is that developers do not need to worry about if a design is good or not, but just about implementing domain specific functions.

Library,Framework and your Code image representation:

https://i.stack.imgur.com/DqCkT.png

KeyDifference:

The key difference between a library and a framework is “Inversion of Control”. When you call a method from a library, you are in control. But with a framework, the control is inverted: the framework calls you. Source.

Relation:

Both of them defined API, which is used for programmers to use. To put those together, we can think of a library as a certain function of an application, a framework as the skeleton of the application, and an API is connector to put those together. A typical development process normally starts with a framework, and fill out functions defined in libraries through API.


Inversion of Control Great in-depth explanation!
Like the picture, sums it up nicelly, and shows also why sometime people get confused, because frameworks often bundle libraries too.
@didibus If you think answer gives good explanation ,you could upvote the answer so that post viewers can easily find the fruitful answer.
So FFmpeg (not the program, but mainly LibAVUtil from the project) is a framework, and libavcodec, etc are libraries?
@MarcusJ - Both LibAVUtil and libavcodec are libraries
J
James Curran

As I've always described it:

A Library is a tool.

A Framework is a way of life.

A library you can use whatever tiny part helps you. A Framework you must commit your entire project to.


This answer makes much more sense after understanding completely both terms.
F
Fizer Khan

From Web developer perspective:

Library can be easily replaceable by another library. But framework cannot. If you don't like jquery date picker library, you can replace with other date picker such as bootstrap date picker or pickadate. If you don't like AngularJS on which you built your product, you cannot just replace with any other frameworks. You have to rewrite your entire code base. Mostly library takes very less learning curve compared to Frameworks. Eg: underscore.js is a library, Ember.js is a framework.


A
Adam Bellaire

I like Cohens answer, but a more technical definition is: Your code calls a library. A framework calls your code. For example a GUI framework calls your code through event-handlers. A web framework calls your code through some request-response model.

This is also called inversion of control - suddenly the framework decides when and how to execute you code rather than the other way around as with libraries. This means that a framework also have a much larger impact on how you have to structure your code.


A
Aniket Kapse

I forget where I saw this definition, but I think it's pretty nice.

A library is a module that you call from your code, and a framework is a module which calls your code.


But libc includes qsort(), which calls your code. I don't think that makes libc a framework.
What is meant by module?
M
Madhuri Patel

A framework can be made out of different libraries. Let's take an example.

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.

https://i.stack.imgur.com/ZbR5p.png

Framework: A collection of libraries that provide unique properties and behavior to your application. (All ingredients) Library: A well-defined set of instructions that provide unique properties and behavior to your data. (Oil on Fish) Plugin : A utility build for a library (ui-router -> AngularJS) or many libraries in combination (date-picker -> bootstrap.css + jQuery) without which your plugin might now work as expected.

P.S. AngularJS is an MVC framework but a JavaScript library. Because I believe Library extends default behavior of native technology (JavaScript in this case).


K
Kon

This is how I think of it (and have seen rationalized by others):

A library is something contained within your code. And a framework is a container for your application.


J
Jin Lim

I will try to explain like you're five. ( No programming term was being used. )

Let's imagine that you had opened a burger restaurant in your city a while ago. But you feel it's so hard to make a burger as a beginner. You were thinking about an easy way to make burgers for customers. Someone told you that If you use framework, you can make bugger easily. and you got to know that there are McDonald Burger Framework and BurgerKing Burger Framework.

If you use McDonald Burger Framework, It's so easy to make Big Mac burger. (but you cannot make Whopper.)

If you use BurgerKing Burger Framework, It's so easy to make Whopper Burger. (however, you cannot make Big Mac)

Anyway, In the end, they are all burgers. An important thing here is, you have to follow their framework's rule to make burgers. otherwise, you feel even harder to make it or won't be able to make it.

And you also heard that there is something called Simple Burger-Patty Library.

If you use this Library, you can make whatever burger patty so easily (X2 speed). It doesn't really matter if you use McDonald Burger Framework or BurgerKing Burger Framework. Either way, you can still use this Simple Burger-Patty Library. (Even you can use this Library without frameworks.)

Do you see the difference between Framework vs Library now?

Once you started using McDonald Burger Framework. It would not be easy to switch to BurgerKing Burger Framework. Since you have to change the whole kitchen.

But Library, It would be much easier to switch others. or you can just not to use it.


We need a vegan-friendly variant of this analogy 🙈
J
Jeff Yates

A library implements functionality for a narrowly-scoped purpose whereas a framework tends to be a collection of libraries providing support for a wider range of features. For example, the library System.Drawing.dll handles drawing functionality, but is only one part of the overall .NET framework.


Good point, that a framework can contain libraries. So, it can be both, a framework in the first and a library in the second instance. As this is the case for many web frameworks like Apache Wicket, that define the processing cycle but also provide a big set of classes that implement concrete ui components.
I believe .NET is a very big library. A library too big to be called library, but still a library. Frameworks enforces a certain design, decides control flow etc with little room to tinker. .NET as such doesn't do that. I believe .NET is a library while ASP.NET MVC is a framework. But MS needs good words to market.
@nawfal .NET has a library. A very large library. Which is still called a library. the BLC (Base Class Library), more precisely the Framework Class Library for .NET Framework, or CoreFX for .NET Core. However, .NET also refers to a platform. Which is called VES (Virtual Execution System), more precisely CLR (Common Language Runtime). You build your code into assemblies for that platform. And it executes your code.
P
Peter Mortensen

I think library is a set of utilities to reach a goal (for example, sockets, cryptography, etc). Framework is library + RUNTIME EINVIRONNEMENT. For example, ASP.NET is a framework: it accepts HTTP requests, create page object, invoke lyfe cicle events, etc. Framework does all this, you write a bit of code which will be run at a specific time of the life cycle of current request!

Anyway, very interestering question!


C
Community

Libraries are for ease of use and efficiency.You can say for example that Zend library helps us accomplish different tasks with its well defined classes and functions.While a framework is something that usually forces a certain way of implementing a solution, like MVC(Model-view-controller)(reference). It is a well-defined system for the distribution of tasks like in MVC.Model contains database side,Views are for UI Interface, and controllers are for Business logic.


A
Adam Bellaire

Your interpretation sounds pretty good to me... A library could be anything that's compiled and self-contained for re-use in other code, there's literally no restriction on its content.

A framework on the other hand is expected to have a range of facilities for use in some specific arena of application development, just like your example, MVC.


P
PhiLho

I think you pinned down quite well the difference: the framework provides a frame in which we do our work... Somehow, it is more "constraining" than a simple library. The framework is also supposed to add consistency to a set of libraries.


G
Gishu

Library - Any set of classes or components that can be used as the client deems fit to accomplish a certain task. Framework - mandates certain guidelines for you to "plug-in" into something bigger than you. You merely provide the pieces specific to your application/requirements in a published-required manner, so that 'the framwework can make your life easy'


D
Diego

I don´t remember the source of this answer (I guess I found it in a .ppt in the internet), but the answer is quite simple.

A Library and a Framework are a set of classes, modules and/or code (depending of the programing language) that can be used in your applications and helps you to solve an especific "problem".

That problem can be log or debuging info in an application, draw charts, create an specific file format (html, pdf, xls), connect to a data base, create a part of an application or a complete application or a code applied to a Design Pattern.

You can have a Framework or a Library to solve all these problems and many more, normaly the frameworks helps you to solve more complex or bigger problems, but that a consecuence of their main difference, not a main definition for both.

The main difference betwen a Library and a Framework is the dependency betwen their own code, in oder words to use a Framework you need to use almost all the classes, modules or code in the FW, but to use a Library you can use one or few classes, modules or code in the lib in your own application

This means that if a Framework has, for example has 50 classes in order to use the framework in an app you need to use, let said, 10-15 or more classes in your code, because that is how is designed a Framework, some classes (objects of that classes) are inputs/parameters for methods in other classes in the framework. See the .NET framework, Spring, or any MVC framework.

But for example a log library, you can just use a Log class in your code, and helps you to solve the "logging problem", that doesn´t mean that the log library doesn't have more classes in his code, like classes to handle files, handle screen outputs, or even data bases, but you never touch/use that classes in your code, and that is the reason of why is a library and not a framework.

And also there are more categories than Frameworks and Libraries, but that is off topic.


F
Favour Felix Chinemerem

I was able to consolidate a couple resources and come up with this, I hope this helps.

This is a "short long" description

What is a Library?

A library is a collection of code blocks (could be in the form of variables, functions, classes, interfaces etc.) that are built by developers to ease the process of software development for other developers that find its relevance.

What is a Framework?

With reference to the definition of a library, we could define a framework as a tool that helps a developer solve a large range of domain-specific problems by providing the developer with necessary libraries in a controlled development environment.


This sounds more like some Framework provider marketing their framework. By this definition I don't see the difference between "framework" and just "a whole bunch of libraries".
M
Maggyero

Based on the definitions given in the book Design Patterns by Erich Gamma et al.:

library: a set of related procedures and classes making up a reusable implementation;

framework: a set of cooperating classes with template methods making up a reusable specification. It sets the control flow and allows to hook into that flow for tailoring the framework to a specific problem by overriding in a subclass the hook methods called by the template methods in the framework classes.

Problem-specific code can use libraries and implement frameworks.


y
yoAlex5

Library vs Framework

Martin Fowler - InversionOfControl

Library and Framework are external code towards yours code. It can be file(e.g. .jar), system code(part of OS) etc.

Library is a set of helpful code. Main focus is on your code. Library solves a narrow range of tasks. For example - utilities, sort, modularisation

your code ->(has) Library API

Framework or Inversion of Control(IoC) container[About] is something more. Framework solves a wide range of tasks(domain specific), you delegates this task to framework. IoC - your code depends on framework logic, events... As a result framework calls your code. It forces your code to stick to it's rules(implement/extend protocol/interface/contract), pass lambdas... For example - Tests, GUI, DI frameworks...

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

[iOS Library vs Framework]

[DIP vs DI vs IoC]


D
David Callanan

Really it depends on what definition you give to the terminology. There's probably a lot of different definitions out there.

I think the following are nice explanations based on what I believe this terminology refers to:

Deterministic Library

A deterministic library holds functions that are deterministic based on either a) function input or b) state that is somehow maintained across function calls.

Should logic be dependency-injected into a deterministic library, such logic must conform to a concrete specification such that the output of the library is not affected.

Example: A collision-detection library which for some reason depends on a sorting function to aid in these calculations. This sorting function can be configured for optimization purposes (e.g. through dependency-injection, compile-time linkage, etc), but must always conform to the same input/output mapping, so that the library itself remains deterministic.

Indeterministic Library

An indeterministic library can hold indeterministic functions by communicating with other external indeterministic libraries that it somehow gained access to.

I generally refer to indeterministic libraries as services.

Example: A poker library which depends on a random-number generator service for shuffling the deck. This is probably a bad example, because, for architectural purposes, we should push the indeterministic aspect of this library to the outside. The poker library could instead become deterministic and unit-testable by taking in a pre-shuffled deck of cards, and it's now the responsibility of the user of this library to shuffle the deck randomly if they so wish.

Framework

A framework is in-between a deterministic and indeterministic library.

Any logic that is dependency-injected into a framework must be deterministic for the lifetime of that function instance, but different function instances of varying logic can be injected on separate executions of framework functions.

Example: Functions that operate on lists such as map, filter, sort, reduce, that expect to take in functions that are deterministic but can have varying logic for different executions. Note that this requirement only exists if these list operations advertise themselves as deterministic. In most languages, list operations wouldn't have this constraint. The core logic of such frameworks are deterministic, but are allowed to accept indeterministic logic at the risk of the user. This is generally a messy scenario to deal with, because output can vary widely due to implementation details of the framework.