liveBook · Manning

Abstract factory design pattern

As explained in Build AI-Enhanced Web Apps

A creational design pattern that provides an interface for creating families of related objects without specifying their concrete classes.

Creates families of related objects through a stable interface while hiding concrete classes. In the Vercel AI SDK, generateText and streamText create language-model instances from different providers. section 3.3.2

The pattern’s implementation has three practical pieces:

  • A shared language-model specification defines the contract that provider implementations must satisfy. section 3.3.2
  • A configuration object selects a provider-specific model while preserving the same generateText interface. section 3.3.2
  • Regular stateless functions can provide the factory behavior; classes are not required. section 3.3.2

Switching providers

Call generateText with openai('gpt-3.5-turbo') or anthropic('claude-v1'), changing the model parameter without changing the text-generation call.

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains the specification, factory roles, and stateless-function alternative, but omits detailed client configuration.
Examples
Demonstrates generateText with OpenAI and Anthropic models using provider packages.
What you can do after
Apply it via the Vercel AI SDK to replace providers or models without changing client generation code.
How it connects
Connects abstract factories with language model specification, provider implementations, and the Vercel AI SDK.

Also explained in

Compare how 3 books explain abstract factory design pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.