With interface oriented
concept, we should put our focus on the service it provided. Say HttpServletRequest
from jakarta.servlet-api, we have different implementation like tomcat, jetty and undertow etc. Both of them has HttpServletRequestImpl
which implements HttpServletRequest
interface. Most of time, we should invoke the service provided by the interface and does NOT care about its underlying.
Another example is the JDBC. We use java.sql.DriverManager
to find and load implementations of java.sql.Driver
. In our program, we did NOT know which underlying database will be connected. But only when they are in the classpath, we can dynamic load them in our Java program with the java.sql.Driver
interface. This is called Service Provider Interface, aka SPI, in Java.
SPI enables developers to extend applications functionalities by adding services implementation, e.g. Jar files, to classpath without modifying the application code base.