Factory Pattern
Factory Pattern
Here we are going to learn factory pattern in python suing a very simple example,
@classmethod - is executed to determine the exact class to be invoked , as in the current scenario, the different switches might have different methods apart from the common method on each device.
The parent class @classmethod is going to determine the right Class , to be in instantiated for the requested devices type.
class Device(object):
@classmethod
def determine_type(cls, type_, *args, **kwargs):
return eval(type_)(*args, **kwargs)
def __init__(self, *args, **kwargs):
self.hostname = kwargs.get("hostname", "localhost")
self.username = kwargs.get("username", "admin")
def __str__(self):
return f"{self.__class__.__name__}:{self.username}@{self.hostname}"
class Cisco(Device):
def __init__(self, *args, **kwargs):
self.vendor = "Cisco"
super().__init__(*args, **kwargs)
class Brocade(Device):
def __init__(self, *args, **kwargs):
self.vendor = "Brocade"
super().__init__(*args, **kwargs)
if __name__ == "__main__":
obj = Device.determine_type(
"Cisco",
hostname="org.cisco.rnd.com",
username="superadmin")
obj1 = Device.determine_type("Brocade")
print(f"Device_1 = {obj}\nDevice_2 = {obj1}")
Output --
$python3 class_method.py
Device_1 = Cisco:superadmin@org.cisco.rnd.com
Device_2 = Brocade:admin@localhost
$
The static method is determining the correct class for the object by the device.
Comments
Post a Comment