Swift World: Design Patterns — Proxy
Today, we will talk about proxy pattern. In this pattern, proxy is an object to help us to access another object. It simply delegates real job to that object or change its behavior. The following figure depicts the roles and their relationships.
Proxy pattern is popularly used in Cocoa which even has a specific NSProxy class in it. Another example is UIApperance protocol and other relevant types.
We will continue using our car system.
protocol Car {
func drive()
}class Sedan: Car {
func drive() {
print("drive a sedan")
}
}
Autonomous car is so hot topic now. So let’s build our own. Actually, it’s not built from scratch but enhance our current car with self-driving system. So it has a internal car instance and delegate the driving to the car. But as an autonomous car, it controls the car automatically. Delegation and change are what a proxy does in this pattern.
class AutonomousCar: Car {
var car: Car init(car: Car) {
self.car = car
} func drive() {
car.drive()
print("by self-driving system")
}
}
Let’s see how to drive it.
//usage
let sedan = Sedan()
let autonomousCar = AutonomousCar(car: sedan)
autonomousCar.drive()