Decorator Pattern

import abcclass Shape(abc.ABC): @abc.abstractmethod def draw(self): passclass Triangle(Shape): def draw(self): print("Triangle")class Circle(Shape): def draw(self): print("Circle")# Decorator was not needed to be inherited from Shape in Python.But you can enforce the…

Continue ReadingDecorator Pattern

State Pattern

# Allowing an object to alter behavior# when its internal state changes so that it appears to change its classclass Phone: def __init__(self): self.ring_state = SoundState() def volumeUp(self): self.ring_state =…

Continue ReadingState Pattern

Strategy Pattern

class File: def __init__(self): self.strategy = None def compress(self): self.strategy.compress()import abcclass CompressionStrategy(abc.ABC): @abc.abstractmethod def compress(cls): passclass ZipCompression(CompressionStrategy): def compress(cls): print("zip Compression")class RarCompression(CompressionStrategy): def compress(cls): print("rar Compression")f = File()f.strategy = ZipCompression()f.compress()f.strategy…

Continue ReadingStrategy Pattern