8 of 20 · System architecture
Facade & Modules
A facade is a small public API in front of a complex subsystem. Putting the subsystem in its own module makes the facade the only thing outsiders can reach, so the compiler enforces the boundary.
How it's wired
- ThanksVault fronts storage and PumpkinPatchClient fronts RPC.
- Envelopes, transports and file I/O are internal to their Swift packages.
| Part | Owns | Looks like |
|---|---|---|
| Facade | The only public entry point | PumpkinPatchClient, ThanksVault |
| Subsystem | Complex internals, free to change | internal types in the package |
| Module | Enforces the boundary | Swift package target |
Swift example: raw subsystem calls vs. a module facade
Without a facade, every caller knows about permissions, SQL, caches and framework errors.
Before vs. after: hiding Contacts behind a facade
Internal types (CNContactStore, SQLite cache, privacy filters) stay internal to the module; callers only see domain types and domain errors.
// ❌ Before: every feature touches CNContactStore, cache and error mapping directly.
// Changing privacy rules or storage means editing 15 callers.
let store = CNContactStore()
let keys = [CNContactGivenNameKey as CNKeyDescriptor]
let raw = try store.unifiedContacts(matching: predicate, keysToFetch: keys)
// ✅ After: one narrow public facade inside a ContactsModule.
public struct ContactSummary: Sendable, Identifiable {
public let id: String
public let displayName: String
}
public enum ContactsError: Error, Sendable {
case permissionDenied
case unavailable
}
public protocol ContactsFacade: Sendable {
func search(_ query: String) async throws(ContactsError) -> [ContactSummary]
}
// CNContactStore, caches, and privacy rules stay `internal` inside ContactsModule.Split facades by capability (contacts, sync, billing) so no single facade grows into an 80-method god object.
What matters in practice
- Without a module boundary a facade is a suggestion. Under deadline, someone imports the transport directly.
- Facades grow into god objects. Split by capability (notes, sync, catalog) instead of one API client with 80 methods.
- The facade is what you version. Behind it you can swap HTTP for gRPC, JSON for protobuf, or add caching without touching a single caller.
When I'd put a facade in a module
| Networking, persistence, analytics, payments | Anything with more than one moving part. |
|---|---|
| A subsystem another team owns | The facade is the contract between teams. |
Compared
Facade vs adapter
| Facade | Adapter | |
|---|---|---|
| Purpose | Hide a subsystem | Make incompatible types fit |
| In this app | PumpkinPatchClient, ThanksVault | UIKitScreen (representable) |
Expert notes
- Make the boundary a compiler error, not a code-review comment.
Check yourself
Answer out loud before you reveal. Recall is what makes it stick.
Explain Facade & Modules in 30 seconds.
- A facade is one small public API in front of a messy subsystem like networking or storage.
- Putting the subsystem in its own Swift package makes everything else internal, so the compiler enforces the boundary instead of code review.
- Callers stay stable while the internals change, say from JSON to protobuf.
- The risk is the facade growing into a god object, so I split facades by capability.
Facade versus Gateway: what's the difference, and who writes each?
- A facade simplifies a complex subsystem for every caller, and whoever owns that subsystem usually writes it.
- A gateway is written by the consumer for its own needs:
protocol PaymentsGatewayspeaks my app's terms and translates to the vendor's. - I'd write gateways for volatile vendor SDKs, like payments or analytics, where swapping later is plausible.
- For a stable system API like
FileManager, the translation layer is cost with no change to absorb.
A third-party analytics SDK is imported in forty files and now must be replaced. How do you prevent that next time?
- I'd wrap it behind a protocol in my app's terms, like
protocol Analytics { func track(_ event: Event) }, so only one file imports the SDK. - Then I'd migrate the forty call sites incrementally while the old and new paths coexist, and swap vendors once nothing imports the SDK directly.
- To prevent a repeat, I'd watch how many files import each third-party module.
- I wouldn't wrap every library, only volatile vendor code, because each wrapper is API you maintain.
Sibling modules in your Swift package need shared helper APIs, but app clients shouldn't use them. How do you expose them?
- I'd mark them
package, which Swift 5.9 added. - Modules in the same package can call them, but code importing the package can't, so app code never builds a dependency on internals you meant to change.
- Before
package, sharing across modules meantpublic, which turned helpers into API. - I'd still keep the
packagesurface small, because sibling modules can couple to it just as tightly as clients would.
Read it in the app's source
Done with Facade & Modules? Mark it complete. Nailing its cards builds this pumpkin, row by row.