Creational Design Pattern — Prototype in iOS-Swift

Prototype pattern refers to creating duplicate object while keeping performance in mind. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

This pattern involves implementing a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly. For example, an object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.

Prototype Pattern in TutorialsPoint

How to implement it in Swift?



class MediumStory: NSCopying {public func copy(with zone: NSZone? = nil) -> Any {return MediumStory(title: self.title, tag: self.tag)}var title : Stringvar tag : Stringinit(title: String, tag: String) {self.title = titleself.tag = tag}}
var prototypePattern = designPatternStory.copy() as! MediumStory
func clone() -> MediumStory{return self.clone() as! MediumStory}var prototypePattern = designPatternStory.clone()

The prototype pattern is implemented correctly if the prototype instance and the clones are standalone objects.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store