SwiftPumpkin

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

send(note)EncodesTransportsApp codeCalls one APIFacadePublic, smallSubsystem AinternalSubsystem Binternal
calls or tellsreports backThe module exports only the facade. The compiler stops anyone reaching the internals.
  • ThanksVault fronts storage and PumpkinPatchClient fronts RPC.
  • Envelopes, transports and file I/O are internal to their Swift packages.
PartOwnsLooks like
FacadeThe only public entry pointPumpkinPatchClient, ThanksVault
SubsystemComplex internals, free to changeinternal types in the package
ModuleEnforces the boundarySwift 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, paymentsAnything with more than one moving part.
A subsystem another team ownsThe facade is the contract between teams.

Compared

Facade vs adapter

FacadeAdapter
PurposeHide a subsystemMake incompatible types fit
In this appPumpkinPatchClient, ThanksVaultUIKitScreen (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.

30-second answer

Explain Facade & Modules in 30 seconds.

System designAsked directly

Facade versus Gateway: what's the difference, and who writes each?

System designAsked indirectly

A third-party analytics SDK is imported in forty files and now must be replaced. How do you prevent that next time?

System designAsked indirectly

Sibling modules in your Swift package need shared helper APIs, but app clients shouldn't use them. How do you expose them?

Read it in the app's source

Done with Facade & Modules? Mark it complete. Nailing its cards builds this pumpkin, row by row.