Monday, October 23, 2023
HomeiOS DevelopmentSwift prototype design sample - The.Swift.Dev.

Swift prototype design sample – The.Swift.Dev.


This can be a creational design sample, it’s helpful when you’ve a really fundamental configuration for an object and you want to provide (clone) these predefined values to a different one. Mainly you are making clones from a prototype objects. 😊😊😊

This strategy has some advantages, one is for instance that you do not have to subclass, however you’ll be able to configure clones individually. This additionally means you can take away a bunch of boilerplate (configuration) code if you’ll use prototypes. 🤔

class Paragraph {

    var font: UIFont
    var coloration: UIColor
    var textual content: String

    init(font: UIFont = UIFont.systemFont(ofSize: 12),
         coloration: UIColor = .darkText,
         textual content: String = "") {

        self.font = font
        self.coloration = coloration
        self.textual content = textual content
    }

    func clone() -> Paragraph {
        return Paragraph(font: self.font, coloration: self.coloration, textual content: self.textual content)
    }
}

let base = Paragraph()

let title = base.clone()
title.font = UIFont.systemFont(ofSize: 18)
title.textual content = "That is the title"

let first = base.clone()
first.textual content = "That is the primary paragraph"

let second = base.clone()
second.textual content = "That is the second paragraph"

As you’ll be able to see the implementation is just some strains of code. You solely want a default initializer and a clone technique. Every thing will likely be pre-configured for the prototype object within the init technique and you can also make your clones utilizing the clone technique, however that is fairly apparent at this level… 🤐

Let’s check out yet another instance:

class Paragraph {

    var font: UIFont
    var coloration: UIColor
    var textual content: String

    init(font: UIFont = UIFont.systemFont(ofSize: 12),
         coloration: UIColor = .darkText,
         textual content: String = "") {

        self.font = font
        self.coloration = coloration
        self.textual content = textual content
    }

    func clone() -> Paragraph {
        return Paragraph(font: self.font, coloration: self.coloration, textual content: self.textual content)
    }
}

let base = Paragraph()

let title = base.clone()
title.font = UIFont.systemFont(ofSize: 18)
title.textual content = "That is the title"

let first = base.clone()
first.textual content = "That is the primary paragraph"

let second = base.clone()
second.textual content = "That is the second paragraph"

The prototype design sample can be useful if you’re planning to have snapshots of a given state. For instance in a drawing app, you may have a form class as a proto, you can begin including paths to it, and sooner or later at time you may create a snapshot from it. You possibly can proceed to work on the brand new object, however this provides you with the flexibility to return to a saved state at any level of time sooner or later. 🎉

That is it in regards to the prototype design sample in Swift, in a nuthsell. 🐿



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments