DEV Community

loading...
Cover image for How classmethod in Python helps in implementing Factory methods ?

How classmethod in Python helps in implementing Factory methods ?

Danyson
My podcast : https://dev.to/byte_with_danyson Website : https://dogealgo.netlify.app/
Updated on ・2 min read

When talking about class method, it bounds to a class rather than to its object. Similar to that of static method, it doesn't requires the creation of the class instance.

The key difference between a static method and a class method is that Static method knows nothing about the class and just deals with the parameters while the Class method works with the class since its parameter is always the class itself.

static method

@staticmethod 
def static_function(arg): 
    return arg
Enter fullscreen mode Exit fullscreen mode

The above example shows us that a decorator @staticmethod is followed by a function accepting a single parameter and returns that parameter arg during its function call.

class method

@classmethod # decorator
def class_function(cls, arg): 
    return cls(arg)
Enter fullscreen mode Exit fullscreen mode

The above example shows us that a decorator @classmethod is followed by a function accepting the class as a parameter along with a parameter named arg so it returns by constructing the class with the parameter arg during its function call.

The Water Tank Factory

from abc import ABC, abstractmethod

class WaterTank(ABC):
    @abstractmethod
    def get_capacity(self):
        pass

class WaterTankFactory(WaterTank):
    def __init__(self, capacity):
        self.__capacity = capacity
    def get_capacity(self):
        return self.__capacity
    @classmethod
    def set_capacity(cls, capacityRequired):
        return cls(capacityRequired)
Enter fullscreen mode Exit fullscreen mode

Let us create a class named WaterTank as an abstract class with an abstract method get_capacity() (getter function for retriving a private variable).

Now let us create a factory class named WaterTankFactory which is going to produce our Water Tanks.

You see as Factories helps us to manufacture products, Factory methods inside a Factory class helps us to create or construct Products in our case we call them Objects or instance of a class.

In our code we are going to use the class method as our factory method.By using the @classmethod before our set_capacity() method, we are saying that the set_capacity() method accepts class as its argument.

Manufacturing our Water Tank

We are going to create two tanks namely tankOne and tankTwo.

tankOne = WaterTankFactory.set_capacity(500)
print(f'Capacity of tankOne is {tankOne.get_capacity()} Litre')

tankTwo = WaterTankFactory.set_capacity(100)
print(f'Capacity of tankTwo is {tankTwo.get_capacity()} Litre')
Enter fullscreen mode Exit fullscreen mode

Directly using the class WaterTankFactory now we can access our factory method set_capacity() which helps us to set the capacity of our Water Tank, as we implemented this factory method using class method of Python 3, it helps us to construct the class and return it as an object. This helps us to avoid passing object instance to the factory method directly from the user by preserving the abstraction and advantage of the users as to be a consumer of what they want.

Output
Capacity of tankOne is 500 Litre
Capacity of tankTwo is 100 Litre

Visit My Personal Blog @ danyson.github.io

Discussion (0)