← Back to blog

2025-10-18

Build an Aggregate Search Using Strategy Pattern

Applying the strategy pattern to combine multiple search providers behind a clean and extensible interface.

An aggregate search can query multiple providers and merge the results. The strategy pattern keeps every provider implementation isolated.

interface SearchStrategy {
  search(query: string): Promise<SearchResult[]>;
}

class AggregateSearch {
  constructor(private readonly strategies: SearchStrategy[]) {}

  async search(query: string) {
    const results = await Promise.all(
      this.strategies.map((strategy) => strategy.search(query))
    );

    return results.flat();
  }
}