Facade
Provides a simplified interface to a complex subsystem.
Context
Your app needs to use 5 services to "publish a post": validate, save, index, notify, audit. The controller ends up knowing about all of them.
Problem
You couple the transport layer to a bunch of internal services. Any change breaks many points.
Solution
A Facade class offers a PublishPost(...) method and orchestrates the services. The client only knows the facade.
#Example in C#
public class PostPublisher
{
private readonly IPostValidator _val;
private readonly IPostRepository _repo;
private readonly ISearchIndexer _idx;
private readonly INotifier _notify;
public PostPublisher(IPostValidator v, IPostRepository r, ISearchIndexer i, INotifier n)
=> (_val, _repo, _idx, _notify) = (v, r, i, n);
public async Task PublishAsync(Post post)
{
_val.Validate(post);
await _repo.SaveAsync(post);
await _idx.IndexAsync(post);
await _notify.NotifySubscribersAsync(post);
}
}When NOT to use it
If the facade becomes a God Object that knows too much: split it.
Tradeoffs
| Pro | Con |
|---|---|
| Reduces client coupling | Can hide subsystem flexibility |
#structural #gof #api