Posts

Adapter Design Pattern

Adapter Design Pattern The Adapter design pattern is used to allow two incompatible interfaces to work together. It involves creating a class (the adapter) that acts as a bridge between two interfaces, converting the interface of one class into another interface that clients expect. In other words, it helps to make different classes or components work together seamlessly. Here’s an example of implementing the Adapter pattern in Python: # Adaptee class LegacyRectangle: def draw( self , x1, y1, x2, y2): print ( f"Drawing a legacy rectangle: ( { x1 } , { y1 } ), ( { x2 } , { y2 } )" ) # Target interface class Shape: def draw( self ): pass # Adapter class RectangleAdapter(Shape): def __init__ ( self , legacy_rectangle): self .legacy_rectangle = legacy_rectangle def draw( self ): x1, y1, x2, y2 = 0 , 0 , 10 , 20 self .legacy_rectangle.draw(x1, y1, x2, y2) def main(): legacy_rectangle = Legac...

Abstract Factory Patter

  Abstract Factor Pattern The Abstract Factory design pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. In Python, you can implement the Abstract Factory pattern using classes and inheritance. Here’s an example of how to implement the Abstract Factory pattern in Python: # Abstract Products class Button: def paint( self ): pass class Checkbox: def paint( self ): pass # Concrete Products for Windows class WindowsButton(Button): def paint( self ): return "Rendering a Windows button" class WindowsCheckbox(Checkbox): def paint( self ): return "Rendering a Windows checkbox" # Concrete Products for Mac class MacButton(Button): def paint( self ): return "Rendering a Mac button" class MacCheckbox(Checkbox): def paint( self ): return "Rendering a Mac checkbox" # Abstr...