Factory Method Design Pattern

Kishara Buddika
2 min readJun 23, 2022

--

According to the Factory Method Pattern, simply creating an object can be done using an interface or abstract class, but the subclasses should choose which class to instantiate. Virtual Constructor is another name for the Factory Method Pattern. This particular design pattern falls under the category of a creational pattern.

The following is how Singleton Pattern and Factory Pattern differ:

In comparison to the singleton design pattern, the factory method operates in a different manner. The factory method design pattern always uses a parameter to determine which instance we want to get. There can be a default without a parameter, it’s totally fine, but usually, we pass a parameter to get. That’s one difference between the singleton and the factory method.

Also in singleton, you always know the type of instance we are going to get. But in the factory method, we don’t know what we are going to get it. It means it can get some sub instances rather than always get parent instances.

In factory method pattern, we don’t see instantiation logic. We always see what we get. We don’t know how that generated, how that instantiation happens.

The comparatively interesting aspect between the singleton and factory method is, that in the factory method the higher classes we may refer to as interfaces, or concrete classes, but as a user, we are not aware of what other parent classes it being inherited or implemented.

Higher-level frameworks are frequently used in the factory method. Then the lower-level users and developers will be the ones to really invoke that.

Let look at a coding example:

public interface Phone {//create a methodvoid showSpec();}//implementing the phone interfacepublic class Huawei implements Phone{@Overridepublic void showSpec() {System.out.println(“Here is a Huawei Phone”);}}//implementing the phone interfacepublic class Samsung implements Phone{@Overridepublic void showSpec() {System.out.println(“Here is a Samsung Phone”);}}//implementing the phone interfacepublic class Iphone implements Phone{@Overridepublic void showSpec() {System.out.println("Here is a IPhone");}}public class PhoneFactory {public Phone getPhone(String shapeType) {if(shapeType == null) {return null;}//checking the inputsif(shapeType.equalsIgnoreCase("Cheap")) {//returning huawei objectreturn new Huawei();} else if(shapeType.equalsIgnoreCase("Middle")) {//returning samsung objectreturn new Samsung();} else if(shapeType.equalsIgnoreCase("Expensive")) {//returning iphone objectreturn new Iphone();}return null;}}public class FactoryDemoPattern {public static void main(String[] args) {//create phonefactory objectPhoneFactory phoneFactory = new PhoneFactory();//calling getPhone method at phonefactory classPhone phone = phoneFactory.getPhone("Expensive");phone.showSpec();}}//output
Here is a IPhone

Thank you for reading! If you enjoyed please leave a clap and follow my page.

Kishara Buddika

--

--

Kishara Buddika

I'm an Associate Software Engineer and a professional Graphic Designer. A blogger with interests in Technology, Creativity and Business. I write for explorers.