Skip to content

brycewang-stanford/Auto-Empirical-Research-Skills / skills/33-Galaxy-Dawn-claude-scholar/skills/architecture-design

architecture-design

Verified

Use only when creating new registrable ML components that require Factory or Registry patterns.

references/registry_pattern.md

View on GitHub
# Registry Pattern

## Overview

The Registry pattern allows components to register themselves via decorators, enabling automatic discovery and centralized management of available types.

## Structure

```python
# In module __init__.py (e.g., data_module/dataset/__init__.py)
from typing import Dict, Callable, TypeVar

T = TypeVar('T')

DATASET_FACTORY: Dict[str, type] = {}

def register_dataset(name: str) -> Callable[[T], T]:
    """Decorator to register dataset classes."""
    def decorator(cls: T) -> T:
        DATASET_FACTORY[name] = cls
        return cls
    return decorator
```

## Usage

```python
# In implementation file (e.g., simple_dataset.py)
from data_module.dataset import register_dataset

@register_dataset("simple")
class SimpleDataset(Dataset):
    def __init__(self, cfg):
        # Implementation
        pass
```

## Benefits

- **Automatic registration**: Components register themselves on import
- **Declarative**: Single decorator line replaces manual registration code
- **Import-time discovery**: Auto-import pattern finds all implementations
- **Type-safe**: Preserves original class type

## Implementation Details

1. Decorator returns the class unchanged (for immediate use)
2. Side effect: adds class to factory dict
3. Name parameter must be unique per module
4. Registration happens at module import time

## Advanced Patterns

### Registration with Config

```python
def register_model(name: str):
    def decorator(cls):
        MODEL_FACTORY[name] = cls
        # Add config validation
        cls._config_schema = getattr(cls, '_config_schema', {})
        return cls
    return decorator
```

### Conditional Registration

```python
def register_dataset(name: str, experimental: bool = False):
    def decorator(cls):
        if not experimental or cfg.enable_experimental:
            DATASET_FACTORY[name] = cls
        return cls
    return decorator
```

### Multi-Registry

```python
# Multiple registries in one module
DATASET_FACTORY = {}
AUGMENTATION_FACTORY = {}

def register_dataset(name: str):
    def decorator(cls):
        DATASET_FACTORY[name] = cls
        return cls
    return decorator

def register_augmentation(name: str):
    def decorator(fn):
        AUGMENTATION_FACTORY[name] = fn
        return fn
    return decorator
```

## Best Practices

- **Unique names**: Use descriptive, unique registration names
- **Documentation**: Document required parameters in class docstring
- **Validation**: Validate config in `__init__`, not in decorator
- **Consistency**: Use same naming convention across modules
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall