Sobes.tech
Junior — Middle

Have you had experience using the Abstract Factory pattern in your projects?

sobes.tech AI

Answer from AI

The Abstract Factory pattern is used to create families of related objects without specifying their concrete classes. In Python projects, I applied this pattern when I needed to create objects of different types but with a common interface, for example, to support different databases or different types of UI components.

Example implementation in Python:

from abc import ABC, abstractmethod

class Button(ABC):
    @abstractmethod
    def click(self):
        pass

class WindowsButton(Button):
    def click(self):
        print("Windows button clicked")

class MacOSButton(Button):
    def click(self):
        print("MacOS button clicked")

class GUIFactory(ABC):
    @abstractmethod
    def create_button(self) -> Button:
        pass

class WindowsFactory(GUIFactory):
    def create_button(self) -> Button:
        return WindowsButton()

class MacOSFactory(GUIFactory):
    def create_button(self) -> Button:
        return MacOSButton()

# Usage
factory = WindowsFactory()
button = factory.create_button()
button.click()  # Windows button clicked

This approach facilitates the extension and maintenance of the code, allowing you to change product families without modifying client code.