A common problem with test-enhanced designs is how to create
Service Stubs in test mode while letting the real thing be there for
production (and for some tests). A couple of my colleagues have
shared their ideas.

Jeremy Stell-Smith showed me an approach based on an Abstract
Factory. All stubbable services are pulled from a single
factory. This example shows such a Persistance class.

In another project, Kraig Parkinson shows a slightly different
take. Rather than use a single Abstract factory, those services that
need stubbing use a prototype.

public class MyFacade {
private static MyFacade prototype;
/**
* Sets the instance of the facade that will be returned by the getInstance method
* used by all clients of the facade.
*/
public static void setFacade(MyFacade newPrototype) {
prototype = newPrototype;
}
/**
* Returns an instance of the facade, using the prototype if set,
* otherwise an instance of the facade is used during normal operation.
*/
public static MyFacade getInstance() {
if (prototype != null)
return prototype;
else
return new MyFacade();
}