Factory Method Pattern

Ravi Bhatt
Coding made easy
Published in
4 min readMay 4, 2021

Hey guys, Hope you are doing good. Welcome to part 3 of the creational design pattern series i.e Factory method pattern. Let’s understand the factory method pattern in the easiest way possible by considering real-world examples.

If you not gone through Part 1 and Part 2 of the creational design pattern, you can read them here.

  1. Part One: Builder Pattern
  2. Part Two: Prototype Pattern

Problem Statement :

Let’s Suppose, You have created one vehicle booking APP, currently, user can only book a car/cab (4 wheeler) through this APP.

Now, your business is growing very fast, you thought to grow this business for BIKE/Scooty (2 wheeler also), as for short rides, the user chooses Bike rather than Cab. You decided to integrate 2 wheeler booking option in your APP. But your main code is written inside the Car class that means your application has tight coupling with the Car class, which means to integrate Bike into your app, you need to change the app code and rewrite the App for Bike class also. This is completely a headache, isn’t it? Writing the common code again and rewrite everything If you want to just add some functionality in your App.

Solution :

Let’s discuss the solution theoretically. Now, this problem can be easily solved by making use of the factory method. But How?

We need to design our app in such a way that, we have to create one superclass that is VehicleBooking .Then createCarBooking &BikeBooking classes that extend VehicleBooking class. Here, Each class has its own factory method i.e getVehicle()that returns Vehicle type object, but CarBooking return Car object and BikeBooking returns Bike object. let’s see the following diagram to understand it more properly.

VehicleBooking cb = new CarBooking();
VehicleBooking bb = new BikeBooking();
Vehicle car = cb.getVehicle();// car object
Vehicle bike = bb.getVehicle();// bike object
car.book(); // book the car
bike.book(); // book the bike

Here, Both the Car and Bike class implement Vehicle interface, both implementing book() method in their own way, such that separating the logic of booking.

Now, our booking App is kind of plug and play, suppose you want to add new vehicle(bus) here then you just need to create a Bus class that extends vehicle class and creates one BusBooking class that should extend VehicleBooking . This BusBooking class has a factory method to create Bus object. The client code that uses the factory method doesn’t see a difference between the actual vehicle returned by the various subclasses. The client treats all vehicles as abstract Vehicle types. Here, the client knows all the Vehicles has book() method, but exactly how it works, is not important for him.

Let’s write some code, for this example. To make this example more interesting, I am adding some more complexity to this problem, Let’s suppose we have two options for each type of vehicle either the user can booking an ordinary vehicle or a prime vehicle.

After going through this example, I think you must have understood the factory method pattern clearly.

When to use the factory method pattern?

  1. Factory Method can be used when you want to provide users of your library or framework with a way to extend its internal components:

The best example for this use case is when you have created one tool to create a UML class diagram and If the user wants to add new functionality of creating a UML sequence diagram, he should not change the interfaces created for extending, He just would be able to implement the framework to extend the functionality.

2. Factory Method can be used when you want to save system resources by reusing existing objects instead of rebuilding them each time.

Thanks, guys for reading the blog completely . Please feel free to ask doubts realted to this blog in the comment sections.

Guys! Good news, Now you clap for the blog up to 50 times in the medium. If you really like the content, please clap at least 30 times and share and follow this publication for more content.

Stay Home, Stay Safe. As now world is suffering from covid 19.

--

--

Ravi Bhatt
Coding made easy

Working as a SDE-3 in Zeta Suite. Has 7.5 years of experience in total.