class CompressionFactory:
compression = {}
def __new__(cls):
cls.compression = {"rar": Rar(), "tar": Tar(), "zip": Zip()}
return cls
@classmethod
def getCompression(cls, method):
return cls.compression[method]
import abc
class File(abc.ABC):
def __init__(self, txt):
self.txt = txt
def __str__(self):
return self.txt
class Compression(abc.ABC):
@abc.abstractmethod
def compress(file):
pass
class Rar:
@staticmethod
def compress(file):
print(f"Rar compresstion {file}")
class Tar:
@staticmethod
def compress(file):
print(f"Tar compresstion {file}")
class Zip:
@staticmethod
def compress(file):
print(f"Zip compresstion {file}")
file = File("Text File")
factory = CompressionFactory()
CompressionFactory.getCompression("rar").compress(file)
CompressionFactory.getCompression("tar").compress(file)
CompressionFactory.getCompression("zip").compress(file)
Related Design Pattern: