Swift World: Design Patterns — Facade

Peng
SwiftWorld
Published in
1 min readMar 16, 2017

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 clap to get this article seen by more people. Please follow me by clicking Follow. As a passionate iOS developer, blogger and open source contributor, I’m also active on Twitter and GitHub.

--

--

Peng
SwiftWorld

Engineers are the artists of our generation.