Library vs. Framework?

What is the difference between a Java Library and a framework? The two concepts are important but sometimes confusing for Java developers.

1. Key Difference and Definition of Library and Framework

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.

framework-vs-library

A library is just a collection of class definitions. The reason behind is simply code reuse, i.e. get the code that has already been written by other developers. The classes and methods 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.

In framework, all the control flow is already there, and there’s a bunch of predefined white spots that you should fill out with your 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.

2. Their 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.

3. Examples

1. How to make a Java library?
2. How to design a framework?

Reference:
Martin Fowler

15 thoughts on “Library vs. Framework?”

  1. While a framework may contain a library of functions which you can call in your code, it also has components which call your code. This is usually done with the Template Method Pattern where the skeleton of an algorithm with default methods is defined in an abstract class and application specific code is defined in a subclass.

  2. I haven’t understand the difference yet. After all, the framework functions will be called by us this way or another…so what is the difference, whether I write “f1()” (as in library) or “do that” (as in framework)..?

  3. Soooo…jQuery is a library, not Framework!

    (The biggest “Deal” of the web coding world finally SOLVED!!)

  4. What did you use to make that graph? It should be “Framework callS you”.

Leave a Comment