Abstract Factory: families of objects that fit together
- Lesson 6 of 32
- 12 min
- Intermediate
- 11 steps
Mix pieces from two databases and the query breaks. Here you will see how a kit always hands you pieces from the same family.
- Families
- 3
- Who picks the family
- main.ts
- request
- response
- failed request
Step 1 of 11
Problem
Two databases, pieces that do not fit
findUser builds a PostgreSQL connection by hand and writes the query with MySQL's parameter placeholder.
Each database writes parameters its own way: PostgreSQL numbers them as $1, $2; MySQL uses ?. With a ?, the query that reaches PostgreSQL does not fit.
What changed
- PgConn and MySQL's placeholder in the same code
- Failed query
Step 2 of 11
Concept
A family: pieces made to go together
A family is a group of objects that depend on each other: the PostgreSQL connection expects PostgreSQL-style parameters.
Picking piece by piece is exactly what allows the mistake. What you want to pick is the whole family, once.
What changed
- PostgreSQL family: PgConn + PgParam
- MySQL family: MyConn + MyParam
Step 3 of 11
Concept
One object that builds the whole family
An Abstract Factory is an interface with one creation method per product: here placeholder() and connect(). Let us call it a kit: SqlKit.
A Factory answers "which class do I build?". An Abstract Factory answers "which family do I build everything from?". It is not "a Factory with an abstract class": what matters is the several products.
What changed
- SqlKit interface
- findUser asks the kit instead of building
Step 4 of 11
Technique
One concrete kit per family
PgKit implements SqlKit and only builds PostgreSQL pieces: its placeholder(1) returns $1 and its connect() returns a PgConn.
No inheritance needed: in TypeScript an object literal with those functions is already a valid kit.
What changed
- PgKit implements SqlKit
- PgKit builds PgConn and PgParam
Step 5 of 11
Solution
findUser only knows SqlKit
Now findUser asks the same kit for the placeholder and the connection. With PgKit the query goes out with $1 and the connection is PostgreSQL: they fit.
Business code names no concrete class. It depends only on the interface.
What changed
- findUser(kit, id)
- Query with $1 to PostgreSQL
Step 6 of 11
Solution
Switching family means switching one object
To use MySQL you pass mysqlKit instead of pgKit. findUser does not change: now it gets ? and a MySQL connection.
The decision does not disappear: it moves to one place, usually where the app starts (main.ts).
What changed
- MySqlKit implements SqlKit
- main.ts: kit = mysqlKit
- findUser: 0 changes
Step 7 of 11
Technique
Where you meet it: DbProviderFactory
In .NET, DbProviderFactory is an abstract class with methods such as CreateConnection(), CreateCommand() and CreateParameter(). SqlClientFactory and OdbcFactory, among others, derive from it.
Code written against the base classes works with whichever provider's factory you pass in. In Java, DocumentBuilderFactory.newInstance() applies a close idea: it finds the implementation at runtime and your code never names the parser class.
What changed
- SqlKit → DbProviderFactory
- Kits → SqlClientFactory, OdbcFactory
Step 8 of 11
Technique
An in-memory family for your tests
A new family does not have to be another database. memKit hands out a fake connection that reads rows from an array.
Your test calls findUser(memKit, 7) and checks the logic without starting a real database. Adding this family took one new kit and zero changes to findUser.
What changed
- MemKit implements SqlKit
- Families: 2 → 3
Step 9 of 11
Trade-off
A new product touches every kit
Now you want transactions: you add begin() to SqlKit. Every kit that implements it has to change: in the example, 3 kits.
That is the deal: adding a family is cheap (one kit), adding a product kind is expensive (every kit).
What changed
- SqlKit.begin(): Transaction
- PgKit, MySqlKit, MemKit: all 3 change
Step 10 of 11
Trade-off
When not to use it
If only one family exists and no second one is planned, the kit is an extra layer: new PgConn(url) reads better.
If the products do not depend on each other, you do not need it either: separate factories or plain dependency injection are simpler.
What changed
- MySqlKit and MemKit
- Families: 3 → 1
Step 11 of 11
Summary
Pick the family once
An Abstract Factory builds several products that must fit and lets business code depend only on the interface.
Switching family is one line in main.ts and a new family is one new kit. The price: every new product touches every kit.
← → step · R replay · Space pause
You will learn
- Why mixing pieces from two families breaks your code (covered)
- What an Abstract Factory is and how it differs from a Factory (covered)
- How business code stops naming concrete classes (covered)
- Where you will meet it: .NET and your own tests (covered)
- What it costs and when not to use it (covered)