It's often useful to be able to quickly and easily switch one implementation of a given feature with another.
This is especially useful when writing unit tests for your code, but the technique isn't
strictly limited to unit tests.

A plugin factory is one way of quickly swapping implementations.
The general idea is to:

define a Java interface for the methods whose implementation you want to be able to swap.

define 2 or more concrete implementations of that interface.

create a corresponding method in a plugin factory class to return one of those
implementations, as defined by some configuration setting.

Using configuration of some kind (often simply a text file), the plugin factory knows which
concrete implementation it's supposed to return to its caller.

It's important for your application to treat the Plugin Factory as the sole source for implementations of the
corresponding interfaces. That is, the rest of your app is not supposed to have direct knowledge of
the concrete implementations. The Plugin Factory is meant to keep that knowledge secret.

A plugin factory can have a number of methods defined, each returning an implementation of a
specific interface.

A recurring theme in object programming is allowing old code to call new code.
A Plugin Factory is simply another variation on that important theme.

Example

As an example, let's take the idea of a fake system clock.
In this case, you want the current time to be defined centrally, in one place. You also want to
be able to swap in various ways of defining the current time, either for testing or for other reasons.

Here's the interface:

package myapp;
/**
Return the time in milliseconds from the Java epoch.
When combined with a TimeZone, such a millisecond value can be
used to create a date-time value.
Variation: one might decide to return a date-time object directly,
instead of a millisecond value.
*/publicinterface TimeSource {
long currentTimeMillis();
}

Here's an example of a particular concrete implementation of the above interface:

Here's a caller that uses a concrete implementation, without knowing its underlying class:

package myapp;
/**
Use a concrete implementation of an interface, without being linked directly to the
the implementing class.
The concrete implementation is known only to the PluginFactory class.
*/publicfinalclass UseFakeSystemClock {
publicvoid doSomethingThatDependsOnTime(){
TimeSource timesource = PluginFactory.getTimeSource();
long currentTime = timesource.currentTimeMillis();
System.out.println("Current millisecond value: " + currentTime);
}
}

Finally, here's a sketch of the Plugin Factory itself. Note that you can add more methods to this class, each
corresponding to a different interface.