Solution: The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate.
public interface Logger { void debug(String message); // implicitly public }
public class FileLogger implements Logger { @Override public void debug(String message) { ... } }
public abstract class AbstractLoggerFactory { public abstract Logger createLogger(); // factory method public Logger getLogger() { Logger logger = createLogger(); ... return logger; } }
public class FileLoggerFactory extends AbstractLoggerFactory { @Override public Logger createLogger() { return new FileLogger(); } }
public class Client { private AbstractLoggerFactory factory = new FileLoggerFactory(); public doSomething(AbstractLoggerFactory factory) { Logger logger = factory.getLogger(); logger.debug("debug message"); } }