Registry Pattern

사용 사례

예시

type ComponentRegistry = {
  [key: string]: React.ComponentType<any>;
};

class SectionRegistry {
  private static instance: SectionRegistry;
  private registry: ComponentRegistry = {};

  private constructor() {}

  public static getInstance(): SectionRegistry {
    if (!SectionRegistry.instance) {
      SectionRegistry.instance = new SectionRegistry();
    }
    return SectionRegistry.instance;
  }

  public register(name: string, component: React.ComponentType<any>): void {
    this.registry[name] = component;
  }

  public get(name: string): React.ComponentType<any> | undefined {
    return this.registry[name];
  }

  public getAll(): ComponentRegistry {
    return { ...this.registry };
  }
}

// 사용 예시
const registry = SectionRegistry.getInstance();

// 섹션 등록
registry.register('HeaderSection', HeaderSection);
registry.register('FooterSection', FooterSection);

// 섹션 사용
const DynamicSection: React.FC<{ name: string }> = ({ name }) => {
  const Section = registry.get(name);
  return Section ? <Section /> : null;
};

// 모든 등록된 섹션 렌더링
const AllSections: React.FC = () => {
  const sections = registry.getAll();
  return (
    <>
      {Object.entries(sections).map(([name, Section]) => (
        <Section key={name} />
      ))}
    </>
  );
};