10 of 22 · 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 |
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 API, and the provider usually writes it for every consumer.
- A Gateway is written by the client for its own needs.
- It wraps an external system behind an interface in your app's terms, like
protocol PaymentsGateway, and translates calls both ways. - Wrapping any foreign API this way pays off, because swapping vendors then touches one type.
- Use a gateway for any API you don't control; the cost is a translation layer you keep in sync with the vendor.
A third-party analytics SDK is imported in forty files and now must be replaced. How do you prevent that next time?
- Wrap it behind a protocol, like
protocol Analytics { func track(_ event: Event) }, so only one file imports the SDK, and keep the concrete type private behind a factory. - Count how many files import each dependency to find where encapsulation is weak.
- The gotcha is over-wrapping every library, which makes the design rigid.
- Wrap volatile third-party code first.
Sibling modules in your Swift package need shared helper APIs, but app clients shouldn't use them. How do you expose them?
- Mark them
package. - It shares the API across modules in the same package without making it public.
- Before it, cross-module use meant
public, so app clients could build unwanted dependencies on internals. - Public symbols are also exported from the binary, which adds code size.
- With
package, the external surface stays small and you can refactor internals freely.