Getty Images Plus
Library vs. framework: How these software artifacts differ
Libraries are low-level components that deliver a specific function, such as establishing a network connection. A framework is a known programing environment, such as Spring Boot.
When a software lead sets out to build a new enterprise application, they must decide which set of libraries and frameworks they want to use. This begs an obvious question: What is the difference between a software library and a framework?
Both a library and a software framework aid in the development of applications. But they key differences between the two relate to the scope of their mandate, and the manner in which they relieve the burden placed on the developer to write code.
Libraries provide components, classes and methods that developers can invoke to perform a specific function. In contrast, a framework provides code that already performs commonly required functions and invokes code provided by a developer when custom functionality is required.
Library vs. framework
A library is set of related, low-level components that developers invoke to achieve a specific outcome. Common library functions include date formatting and establishing a network connection.
Frameworks address a higher-level concern. Frameworks provide an established programming environment that is itself built on top of low-level libraries.
For example, a framework might deal with how to best map all of the objects in an application into tables in an associated database. It could also deal with how to deliver a rich, web-based experience to an end user.
Software library examples
One example of a library is the Java Date and Time API. It is a set of classes and interfaces that define properties and methods that developers can use to format dates, perform time zone conversions and provide global calendar support. Java developers instantiate Date and Time classes when needed and invoke the library's methods in any manner they see fit.
Another Java library example is the popular java.net network I/O package. This library is comprised of dozens of classes and interfaces that developers use to open network ports, handle incoming connections and send data to interconnected clients.
With nothing more than the java.net package, a software developer could implement all of the necessary logic required to handle REST-based web services or HTML-based request-response cycles over the HTTP protocol. The java.net library provides a set of low-level APIs that any developer can use to develop applications that communicate across a network.
Popular Java libraries
Other popular Java libraries, including ones that already leverage a Java framework such as Spring Boot, JHipster or Vaadin, include:
- Apache Commons Math
- BouncyCastle for cryptography
- Java Advanced Imaging
- Java Speech
- Java 3D
- Apache Collections
- Jackson for JSON and XML
- Java Networking
The role of software frameworks
Enterprise projects often require developers to provide a RESTful set of APIs for external clients to integrate with online applications. However, many developers have not only solved the problem of how to use the low-level java.net library to handle RESTful API calls, but they have shared their work as open source projects. This is the essence of a software framework. It is an existing project that solves a common and challenging problem, which developers can use within their own projects.
There are often many frameworks to choose from within a given problem domain. In the Java ecosystem, there are a number of popular frameworks that use the standard Java and Jakarta EE libraries to solve the problem of how to best handle a REST-based request-response cycle, including:
- Spring Boot
- Apache CFX
Frameworks have opinions
Another key feature that distinguishes a framework from a library is the fact that the latter typically includes an opinion about how a given problem should be solved, what the scope of the problem is and how to address the goals of the framework in the most efficient and effective manner.
Frameworks will compete over a variety of non-functional requirements such as ease of use, performance, pluggability and compatibility.
Frameworks and inversion of control
Finally, a key distinguishing factor between libraries and frameworks is the comparative inversion of control (IoC) between the two. IoC is often referred to as the Hollywood Principle. Unlike a library, a framework takes a "don't call us, we'll call you" approach to the code a developer writes.
To develop an application with a library, a developer must instantiate library components and invoke the methods of the classes and interfaces that exist therein. For example, if a developer wanted to use the java.net library to create their own RESTful application, they would need to invoke a variety of APIs to open ports, handle HTTP connections, format data into XML or JSON representations and communicate back and forth across the wire when a client initiates a request.
On the other hand, a framework takes care of all of the underlying plumbing required to support the project's core functionality. A framework will invoke the code provided by the developer only when the need arises.
IoC and Spring Boot
A RESTful web service written in Spring Boot nicely exemplifies the IoC's Hollywood Principle.
With a Spring Boot RESTful web service, a developer need only code the logic to be performed when an HTTP-based GET invocation occurs. When that GET invocation occurs, the Spring Boot framework handles it, interprets it, converts the data exchange format into JSON or XML and then routes the RESTful request to the developer's business logic.
The developer only needs to write the business logic. It is the framework that decides when the business logic gets called.
IoC frameworks are commonplace in Java. The following is a list of ten popular examples.
- JavaServer Faces
- Play for Scala
Frameworks and libraries together
While it's true that frameworks and libraries have different functions, both are ways to help devs get more done. No software professional wants to waste time and reinvent the wheel, when many common programming challenges have already been solved and shared in the form of permissively licensed, open source projects.
Whenever a you initiate a new software development project, it is important to select a proven and established software development framework that will support you. At the same time, be consistent with your personal development and design philosophies. And in those rare instances where a specialized corner case falls outside of the purview of the selected framework, standard libraries are always there to help your developers bridge the gaps.