Factory Method Design Pattern in Java

A Design Pattern is a typical solution to the commonly occurring problems while designing software. Each design pattern is a solution proven to be efficient by software development experts worldwide.

The Factory Method is a creational design pattern that provides an interface for creating objects in a superclass but allows the subclasses to alter the type of objects that will be created.

This article explains one of the creational design patterns, Factory Method, in Java with an example.

Factory Method and  Java

The Factory Method is a popularly used creational design pattern in Java. It is used to provide a huge level of adaptability to code. This pattern is present is the core Java libraries too. Some of them are java.util.Calender.getInstance() and java.nio.charset.Charset.forName().

The Factory Method pattern has the following structure

factory method pattern

An Example

Assume a scenario where every person has to be greeted by some software based on their mother tongue, say either French or German. While it is possible to it without design pattern, using the Factory design pattern can make it more structural. The above scenario, when implemented with the Factory design pattern, will have the following structure.

https://drive.google.com/file/d/1YxvH4uH2SNma37DB5Qdz347cjjJuPtmz/view?usp=sharing

The method createGreetingAPI() is the factory method here. This method is overridden by the subclasses of the Greeting class, FrenchGreeting and GermanGreeting to create FrenchGreetingAPI and GermanGreetingAPI respectively. Then upon invoking the greet() method of an API returns a greeting in the corresponding language.

The program below is the Java implementation of the above scenario using the Factory Method design pattern.

interface GreetingAPI {
    String greet();
}

class FrenchGreetingAPI implements GreetingAPI {
    public String greet() {
        return "Comment ça va?";
    }
}

class GermanGreetingAPI implements GreetingAPI {
    public String greet() {
        return "Wie geht's?";
    }
}

abstract class Greeting {
    public String greetString() {
        GreetingAPI api = createGreetingAPI();
        return api.greet();
    }
    public abstract GreetingAPI createGreetingAPI();
}

class FrenchGreeting extends Greeting {
    @Override
    public GreetingAPI createGreetingAPI() {
        return new FrenchGreetingAPI();
    }
}

class GermanGreeting extends Greeting {
    @Override
    public GreetingAPI createGreetingAPI() {
        return new GermanGreetingAPI();
    }
}

class Example {
    public static void main(String[] args) {
        Greeting g;
        g = new FrenchGreeting();
        System.out.println("French: "+g.greetString());
        g = new GermanGreeting();
        System.out.println("German: "+g.greetString());
    }
}

The class Example is called the client code because it uses the Factory Method. The Example class first creates FrenchGreeting and invokes the greetString() method that returns the greeting in French. Then, it does the same with GermanGreeting. The above program produces the following output.

French: Comment ça va?
German: Wie geht's?

Leave a Reply

Your email address will not be published. Required fields are marked *