opencodez

Java Creational Design Patterns – Factory Method Pattern

In this article, we will discuss Factory Method Pattern. This pattern is organized under creational pattern as it deals with the creation of the object.

This design pattern is based on one of the OOPs concept ie. Encapsulation. Generally, we write object creation code on client side program but in factory pattern, we encapsulate object creation code inside factory method. So depending on the data provided to the factory, it can return an object of one of several possible classes listed in a program. You can consider using this pattern when:

xHere are ALL Java Design Patterns, explained in detail with examplesx

Factory Method Pattern by Example

This pattern can be understood with a simple example as below:

We will define an interface that will be implemented by our different concrete classes. Here we have a Country interface which has 2 methods to be implemented.

public interface Country {
	
	public String getArea();

	public String getCapital();
	
	default String getClassAsString() {
		return this.getClass().getSimpleName() + " [Area=" + getArea() + ", Capital=" + getCapital() + "]";
	}
}

Note: Here we have tried to use the latest Java 8 feature of default methods. You can read about them in here.

Now let us define a couple of concrete classes which implements an interface.

public class India implements Country {

	@Override
	public String getArea() {
		return "3.287 million km²";
	}

	@Override
	public String getCapital() {
		return "New Delhi";
	}

	@Override
	public String toString() {
		return getClassAsString();
	}
}

and another x

public class Japan implements Country {

	@Override
	public String getArea() {
		return "377,962 km²";
	}

	@Override
	public String getCapital() {
		return "Tokyo";
	}

	@Override
	public String toString() {
		return getClassAsString();
	}
}

The job of the factory method is only created and return objects based on criteria. So it would be best if we keep the method as static.

public class Factory {
	
	public static Country getCountry(String c) {
		Country country = null;
		if(c.equalsIgnoreCase("India")) {
			country = new India();
		} else if(c.equalsIgnoreCase("Japan")) {
			country = new Japan();
		}
		return country;
	}
}

Running the Example

public class FactoryDemo {

	public static void main(String[] args) {
		String input = "India";
		Country ci = Factory.getCountry(input);
		System.out.println(ci);
		
		input = "Japan";
		Country cj = Factory.getCountry(input);
		System.out.println(cj);
	}
}

Output:

India [Area=3.287 million km², Capital=New Delhi]
Japan [Area=377,962 km², Capital=Tokyo]

In the demo, you can see that the objects are created in the factory method and returned to the client or user for further usage.

Advantages

Conclusion

In this article, we understood Factory Method Pattern with the help of a simple example. The source code is available in our Github repository.

Download Code

xHere are ALL Java Design Patterns, explained in detail with examplesx