The Difference Between a Library and a Framework

The Difference Between a Library and a Framework

·

2 min read

Introduction

You’ve probably heard of the terms “library” and “framework” being often used interchangeably. This may be because they both have a similar purpose; both libraries and frameworks provide reusable code written by someone else that solves a common problem. However, there is a distinct difference between the two.

Main Difference Between a Library and a Framework

The main difference between a library and a framework is determined by who controls the development process, which is known as inversion of control. In other words, inversion of control defines the relationship between the code and a library and framework, respectively.

The diagram below summarizes the difference between a library and framework using the inversion of control property:

library-v-framework(1).png

Your Code Calls a Library

You, the developer, control a library by calling specific functions from a library to insert into your code depending on your app’s needs.

Let’s use the analogy of baking a cake. You, the baker, have control over the baking process. You have a recipe and some of the ingredients, which represent your program. However, you need to go to the grocery store to buy the rest of the ingredients, which represents a library.

A Framework Calls Your Code

On the other hand, the control is inverted; the framework is in control of your code by having defined an architectural structure that you must follow when developing your app and the framework will call your code when needed.

Let’s continue using the baking analogy used in the previous section. Rather than baking the cake yourself, you go to a bakery to buy a cake. You get to choose the size, flavor, shape, and decorations, and the bakery will bake the cake for you according to your specifications. The bakery represents a framework, and your customization represents your code.

Summary

There are other subtle differences between a library and framework, but the main difference between the two is defined by the inversion of control property, which defines who controls the development process. In summary:

  • Your code controls the library.
  • The framework controls your code.

Feel free to comment below with any thoughts or questions!