Swift World: Design Patterns — Facade

Literally, facade means

the face of a building, especially the principal front that looks onto a street or open space.
from Facade — Google Search

Similarly, as design pattern facade defines an simpler interface to an complex subsystem. For example, in our car factory, we have different departments to produce different components like engine, body, and accessories. As client, we don’t care how every department does produce its own job. We just create a factory instance and get it to work.

class Engine {
func produceEngine() {
print("prodce engine")
}
}
class Body {
func produceBody() {
print("prodce body")
}
}
class Accessories {
func produceAccessories() {
print("prodce accessories")
}
}

So we build a facade to provide a simple interface.

class FactoryFacade {
let engine = Engine()
let body = Body()
let accessories = Accessories()
    func produceCar() {
engine.produceEngine()
body.produceBody()
accessories.produceAccessories()
}
}

Then use the factory directly.

let factoryFacade = FactoryFacade()
factoryFacade.produceCar()

The structure figure is clear.

Thanks for your time. Please click the ❤ button to get this article seen by more people.