From the course: Practical Design Patterns in Swift

Unlock the full course today

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

Sharing of common data

Sharing of common data - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Sharing of common data

- [Instructor] The flyweight design pattern saves memory and reduces the creation costs for similar objects. We can apply it if we have numerous objects that rely on the same immutable data. The flyweight pattern minimizes memory usage by sharing the common data among multiple objects. To make it work we need to separate the parts that can change from the immutable set of properties. The unchanging parts get stored in the flyweight and are protected from changes. We need to extract the properties that might change. The clients can manage these mutable properties and pass them through the flyweight object when necessary. The shared immutable part is called Intrinsic state and the properties that can change represent the Extrinsic state of the object. Here is an example to illustrate the process of identifying the extrinsic and intrinsic state. In computer games we often see objects that look similar. Plants, rocks, buildings, vehicles and so on. Let's take a concrete case. A Spaceship.…

Contents