12.8 C
Canberra
Sunday, October 26, 2025

Swift prototype design sample – The.Swift.Dev.



· 1 min learn


The prototype design sample is used to create clones of a base object, so let’s examine some sensible examples written in Swift.

This can also be a creational design sample, it’s helpful when you could have a really primary configuration for an object and also you’d like to provide (clone) these predefined values to a different one. Principally you’re making clones from a prototype objects. 😊😊😊

This strategy has some advantages, one is for instance that you simply don’t must subclass, however you may configure clones individually. This additionally means that you could take away a bunch of boilerplate (configuration) code if you will 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 may see the implementation is just some strains of code. You solely want a default initializer and a clone technique. Every part will likely be pre-configured for the prototype object within the init technique and you may make your clones utilizing the clone technique, however that’s fairly apparent at this level… 🤐

Let’s check out yet one more 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 also 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 in some unspecified time in the future at time you may create a snapshot from it. You may proceed to work on the brand new object, however this will provide you with the flexibility to return to a saved state at any level of time sooner or later. 🎉

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

Associated posts


· 5 min learn


On this article I’m going to indicate you the way to implement a primary occasion processing system in your modular Swift software.


· 4 min learn


Study the iterator design sample through the use of some customized sequences, conforming to the IteratorProtocol from the Swift commonplace library.


· 4 min learn


Discover ways to use lazy properties in Swift to enhance efficiency, keep away from optionals or simply to make the init course of extra clear.


· 5 min learn


Newbie’s information about optics in Swift. Discover ways to use lenses and prisms to control objects utilizing a purposeful strategy.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles