The Supplier
interface is simply an abstraction of a no-arg function that returns a value… it is a means of getting some instance or instances of an object. Since it is so general, it can be used as many things. Jared explained how the Multimaps
factories utilize it as a factory for creating a new instance of a Collection
of some type for values.
Given the simplicity of the interface, it also allows for some very powerful decoration of a Supplier
‘s behavior by wrapping it in another Supplier
that alters its behavior somehow. Memoization is one example of that. I’ve used the Suppliers.memoizeWithExpiration
method myself as an easy way to make it so some data will only be read from a server at most once in a given period of time.
I’d also recommend taking a look at Guice and how the Provider
interface is used in it. Provider
is exactly equivalent to Supplier
and is central to how Guice works.
Provider
allows users to define a custom way of creating new objects of a given class. Users can write aget()
method which can execute whatever code is needed to create a new object, so they aren’t limited to having Guice use constructors alone to create objects. Here, they are using it to define a custom factory for new instance of an object.- Guice allows injection of a
Provider
of any dependency. This may return a new instance every timeget()
is called or it may always return a single instance or anything in between, depending on how the binding theProvider
represents is scoped. This also allows for “lazy instantiation” of dependencies… theProvider
gives a class a means of creating an object without needing to actually create the object ahead of time. An instance of the object does not need to be created until when, and if,get()
is called. - As indicated above,
Provider
s form the basis of scoping in Guice. If you take a look at the Scope interface, you’ll notice its single methodProvider<T> scope(Key<T> key, Provider<T> unscoped)
is defined in terms ofProvider
s. This method takes something that creates a new instance of an object (theProvider<T> unscoped
) and returns aProvider<T>
based on that which applies whatever policy the scope defines, potentially returning some cached instance of the object rather than creating a new one. The defaultNO_SCOPE
scope simply passes along theunscoped
provider, meaning a new instance will be created each time. TheSINGLETON
scope caches the result of the first call tounscoped.get()
and thereafter returns that single instance, ensuring that everything that depends on the singleton-scoped object gets a reference to that single object. Note that theProvider
returned by theSINGLETON
scope’sscope
method does essentially the same thing as theSupplier
returned bySuppliers.memoize
(though it’s a bit more complicated).