Factory Method – Python Design Patterns

Factory Method is a Creational Design Pattern that allows an interface or a class to create an object, but let subclasses decide which class or object to instantiate. Using the Factory method, we have the best ways to create an object. Here, objects are created without exposing the logic to the client and for creating the new type of the object, the client uses the same common interface.

Problems we face without Factory Method:

Imagine you are having your own startup which provides ridesharing in the different parts of the country. The initial version of the app only provides the Two-Wheeler ridesharing but as time passes, your app becomes popular and now you want to add Three and Four-Wheeler ridesharing also.
It’s a piece of great news! but what about the software developers of your startup. They have to change the whole code because now the most part of the code is coupled with the Two-Wheeler class and developers have to make changes into the entire codebase.
After done with all these changes, either the developers end with the messy code or with the resignation letter.

Localizer app

Diagrammatic representation of Problems without using Factory Method

Let’s understand the concept with one more example which is related to the translations and localization of the different languages.
Suppose we have created an app whose main purpose is to translate one language into another and currently our app works with 10 languages only. Now our app has become widely popular among people but the demand has grown suddenly to include 5 more languages.
It’s a piece of great news! only for the owner not for the developers. They have to change the whole code because now the most part of the code is coupled with the existing languages only and that’s why developers have to make changes into the entire codebase which is really a difficult task to do.

Let’s look at the code for the problem which we may face without using the factory method.

Note: Following code is written without using the Factory method.

filter_none

editclose

play_arrow

linkbrightness_4code

# Python Code for Object

# Oriented Concepts without

# using Factory method

classFrenchLocalizer:

""" it simply returns the french version """

def__init__(self):

self.translations ={"car": "voiture", "bike": "bicyclette",

"cycle":"cyclette"}

deflocalize(self, message):

"""change the message using translations"""

returnself.translations.get(msg, msg)

classSpanishLocalizer:

"""it simply returns the spanish version"""

def__init__(self):

self.translations ={"car": "coche", "bike": "bicicleta",

"cycle":"ciclo"}

deflocalize(self, msg):

"""change the message using translations"""

returnself.translations.get(msg, msg)

classEnglishLocalizer:

"""Simply return the same message"""

deflocalize(self, msg):

returnmsg

if__name__ =="__main__":

# main method to call others

f =FrenchLocalizer()

e =EnglishLocalizer()

s =SpanishLocalizer()

# list of strings

message =["car", "bike", "cycle"]

formsg inmessage:

print(f.localize(msg))

print(e.localize(msg))

print(s.localize(msg))

chevron_right

filter_none

Solution by Factory Method:

Its solution is to replace the straight forward object construction calls with calls to the special factory method. Actually there will be no difference in the object creation but they are being called within the factory method.

For example: Our Two_Wheeler, Three_Wheeler, and Four_wheeler classes should implement the ridesharing interface which will declare a method called ride. Each class will implement this method uniquely.

solution_factory_diagram

Now let us understand the factory method with the help of an example:

filter_none

editclose

play_arrow

linkbrightness_4code

# Python Code for factory method

# it comes under the creational

# Design Pattern

classFrenchLocalizer:

""" it simply returns the french version """

def__init__(self):

self.translations ={"car": "voiture", "bike": "bicyclette",

"cycle":"cyclette"}

deflocalize(self, message):

"""change the message using translations"""

returnself.translations.get(msg, msg)

classSpanishLocalizer:

"""it simply returns the spanish version"""

def__init__(self):

self.translations ={"car": "coche", "bike": "bicicleta",

"cycle":"ciclo"}

deflocalize(self, msg):

"""change the message using translations"""

returnself.translations.get(msg, msg)

classEnglishLocalizer:

"""Simply return the same message"""

deflocalize(self, msg):

returnmsg

defFactory(language ="English"):

"""Factory Method"""

localizers ={

"French": FrenchLocalizer,

"English": EnglishLocalizer,

"Spanish": SpanishLocalizer,

}

returnlocalizers[language]()

if__name__ =="__main__":

f =Factory("French")

e =Factory("English")

s =Factory("Spanish")

message =["car", "bike", "cycle"]

formsg inmessage:

print(f.localize(msg))

print(e.localize(msg))

print(s.localize(msg))

chevron_right

filter_none

Class Diagram for Factory Method:

Let’s look at the class diagram considering the example of ride sharing.

Factory_pattern_class_diagrma

Advantages of using Factory method:

We can easily add the new types of products without disturbing the existing client code.

Generally, tight coupling is being avoided between the products and the creator classes and objects.

Disadvantages of using Factory method:

To create a particluar concrete product object, client might have to sub-class the creator class.

You end up with huge number of small files i.e, cluttering of the files.

Applicability :

In a Graphics system, depending upon the user’s input it can draw different shapes like Rectangle, Square, Circle, etc. But for the ease of both developers as well as the client, we can use the factory method to create the instance depending upon the user’s input. Then we don’t have to change the client code for adding a new shape.

In a Hotel booking site, we can book a slot for 1 room, 2 rooms, 3 rooms, etc. Here user can input the number of rooms he wants to book. Using the factory method, we can create a factory class AnyRooms which will help us to create the instance depending upon the user’s input. Again we don’t have to change the client’s code for adding the new facility.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.