Standards and boilerplate for mandatory patterns (Singleton, Observer, Strategy).
Standardize mandatory patterns to ensure consistency and thread-safety.
Use for Config, Services. threading.RLock is required.
import threading
from typing import TypeVar, Generic, Optional
T = TypeVar('T')
class SingletonMeta(type):
_instances: dict[type, object] = {}
_lock: threading.RLock = threading.RLock()
def __call__(cls, *args, **kwargs):
with cls._lock:
if cls not in cls._instances:
cls._instances[cls] = super().__call__(*args, **kwargs)
return cls._instances[cls]
class MyService(metaclass=SingletonMeta):
pass
Decouple UI and Logic.
from enum import Enum
from typing import Callable, List, Dict
class EventType(Enum):
FILE_UPDATED = "file.updated"
ERROR_OCCURRED = "error.occurred"
class EventBus(metaclass=SingletonMeta):
def __init__(self):
self._subscribers: Dict[EventType, List[Callable]] = {}
def subscribe(self, event_type: EventType, callback: Callable):
if event_type not in self._subscribers:
self._subscribers[event_type] = []
self._subscribers[event_type].append(callback)
def publish(self, event_type: EventType, data: dict = None):
if event_type in self._subscribers:
for callback in self._subscribers[event_type]:
callback(data)
Abstract data access.
from abc import ABC, abstractmethod
from typing import List, Generic, TypeVar
T = TypeVar('T')
class IRepository(ABC, Generic[T]):
@abstractmethod
def get(self, id: str) -> Optional[T]:
pass
@abstractmethod
def add(self, item: T) -> None:
pass