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.

Purpose: Cloning

Purpose: Cloning - Swift Tutorial

From the course: Practical Design Patterns in Swift

Start my 1-month free trial

Purpose: Cloning

- [Instructor] The prototype pattern can help us when creating multiple instances of the same type is expensive or inefficient. Consider the following example. You need to construct several objects of a given type. During instantiation, each object needs to open the same large file and load its contents in memory. If instantiating one object takes 100 milliseconds, as we know file IO can be really slow, then we need one second to create 10 objects, and creating 100 objects takes 10 seconds. The time required to create the objects grows linearly. We could optimize the object creation by avoiding the opening and reading of the file each time we instantiate an object. The prototype add-on addresses this issue by cloning the objects that are expensive to create. We construct the first object as usual. We can't avoid this step. The very first object, also known as the prototype, is required. However, all consecutive instances get created by copying the prototype. Thus, we avoid the…

Contents