Nov 10, 2018

Service Provider Interface (SPI)

Service Provider Interface (SPI) is an API intended to be implemented or extended by a third party. It can be used to enable framework extension and replaceable components.

The API is the description of classes/interfaces/methods/... that you call and use. The SPI is a special API that you extend and implement. The API tells you what a specific class/method does for you and the SPI focuses on what you must do to conform.

Use a context object (or argument object) for your SPI instead of dozens of overloading methods.

For example:
interface MessageContext {  String message;  Integer id;  MessageSource source;}
interface EventListener {  void message(MessageContext context);}
is much better and more flexible than:
interface EventListener
{    
void message(String message) {    message(message, null, null);  }   
void message(    String message,    Integer id,    MessageSource source  );
}