Imagine that your application is very complicated and it happened that you use two logging providers – one is Log4J and another is Java Logging. Your co-worker got great idea to have specification of which of the providers to use in configuration file. As you wrap all logging logic behind some ILogger interface, you also want to hide exact logging provider from client code and pass creating logic to some separate class.

FACTORY METHOD

For me this design patter is the most familiar and easiest. I’m sure that most of the readers have seen it many times. So the intent of the Factory Method is to hide exact class that should be created basing on some conditions.
In our example classes that should be created are Log4J or JavaLogging, they both implement interface ILogger, that is widely used in your application.

public interface ILogger {
    void LogToDatabase(String message);
    void LogToFile(String message);
    void LogToConsole(String message);
}

As you already guess it might be that in future you will decide to use another logging provider. As we already described in our scenario we read which logging provider to use from some configuration. For not exposing exact classes we delegate this work to LoggerProviderFactory. Here is usage:

    public void Run(){
        LoggerProviderFactory.LoggingProviders providerType =
                getTypeOfLoggingProviderFromConfigFile();

        ILogger logger = LoggerProviderFactory.GetLoggingProvider(providerType);

        logger.LogToConsole(“Hello Factory Method Design Pattern.”);
    }

    private LoggerProviderFactory.LoggingProviders getTypeOfLoggingProviderFromConfigFile() {
        return LoggerProviderFactory.LoggingProviders.Log4J;
    }

What we are getting back from GetLoggingProvider method is interface. Factory Method decides which of the concretes return basing on the input parameter, which in our case is enum.

Here is implementation of Factory Method:

public class LoggerProviderFactory {

    public enum LoggingProviders{
        JavaLogging,
        Log4J
    }

    // this is our factory method…
    public static ILogger GetLoggingProvider(LoggingProviders logProviders)
    {
        switch(logProviders){
            case JavaLogging:
                return new JavaLogging();
            case Log4J:
                return new Log4J();
            default:
                return new JavaLogging();
        }
    }
}

Because my hardcoded configuration logic returns me Log4J and since implementation of LogToConsole of Log4J concrete logger looks like:

    public void LogToConsole(String message) {
        System.out.println(“Log4J: “ + message);
    }

I’m getting this as output: Log4J: Hello Factory Method Design Pattern.

Here is my drawing try:

If you liked or disliked this blog post, just let me know.

Check out my Design Patterns Table.

If you haven't subsribed yet, you can subsribe below: