From the course: Practical Design Patterns in Swift

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

The surrogate

The surrogate - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

The surrogate

- [Instructor] The proxy pattern is about managing and controlling access to specific objects. The proxy acts as a placeholder for the proxied instance and introduces an additional level of interaction to support controlled, remote, or delayed access. Why would we need that additional level of indirection? There are certain situations when providing a surrogate is a better choice than accessing the object directly. The first case, the object is a resource hog, and you want to delay its creation until it's first accessed. Lazy properties are a perfect example of this delayed initialization. Case number two, you rely on a remote resource, like a web service. The remote proxy acts as a local placeholder to control and optimize the access to the real resource. For example, a remote proxy may batch network calls before accessing RESTful service or return cached data instead of firing a network request. Case number three, you use protected or sensitive resources. The protective proxy…

Contents