Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Swift

Swift prototype design pattern – The.Swift.Dev.

learningcode_x1mckf by learningcode_x1mckf
October 5, 2022
in Swift
0
Swift prototype design pattern – The.Swift.Dev.
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


You might also like

The abstract Vapor service factory design pattern

SwiftNIO tutorial – The echo server

Introducing – Vapor cheatsheet – The.Swift.Dev.

This can be a creational design sample, it’s helpful when you will have a really fundamental configuration for an object and you would like to present (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 possibly can configure clones individually. This additionally means you can 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 possibly can see the implementation is only a few strains of code. You solely want a default initializer and a clone technique. All the things can be pre-configured for the prototype object within the init technique and you may 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 helpful if you’re planning to have snapshots of a given state. For instance in a drawing app, you can 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 can create a snapshot from it. You’ll be able to proceed to work on the brand new object, however this offers you the flexibility to return to a saved state at any level of time sooner or later. ๐ŸŽ‰

That is it concerning the prototype design sample in Swift, in a nuthsell. ๐Ÿฟ



Source link

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

The abstract Vapor service factory design pattern

by learningcode_x1mckf
February 1, 2023
0
The abstract Vapor service factory design pattern

I've written a number of articles about manufacturing unit design patterns on my weblog and this time I might like to speak a couple of particular one, which...

Read more

SwiftNIO tutorial – The echo server

by learningcode_x1mckf
January 27, 2023
0
SwiftNIO tutorial – The echo server

Intoducing SwiftNIO In the event you used a excessive degree net framework, corresponding to Vapor, up to now, you would possibly had some interplay with occasion loops...

Read more

Introducing – Vapor cheatsheet – The.Swift.Dev.

by learningcode_x1mckf
January 23, 2023
0
Introducing – Vapor cheatsheet – The.Swift.Dev.

Out there on Gumroad Thanks for supporting my work by buying the cheatsheet. ๐Ÿ™ Download now A whole Vapor framework reference for novices. greater than...

Read more

iCloud Programming Tutorial for iOS: An Introduction

by learningcode_x1mckf
January 18, 2023
0
iCloud Programming Tutorial for iOS: An Introduction

Editorโ€™s observe: This week, we work with Ziad Tamim once more to provide you an introduction of iCloud programming. Youโ€™ll learn to save and retrieve knowledge from iCloud.On...

Read more

Easy multipart file upload for Swift

by learningcode_x1mckf
January 18, 2023
0
Easy multipart file upload for Swift

I imagine that you've got already heard in regards to the well-known multipart-data add method that everybody likes to add recordsdata and submit type knowledge, but when not,...

Read more
Next Post
Quick Tip: How to Use the Spread Operator in JavaScript

Quick Tip: How to Use the Spread Operator in JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Java array size, length and loop examples

How do I find the Java String length?

November 23, 2022
Senior Java Developer at Datonomy Solutions – Gauteng Johannesburg North

Senior Java Developer at Datonomy Solutions – Gauteng Johannesburg North

November 22, 2022
Picking and playing videos in Swift

Picking and playing videos in Swift

September 28, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

ยฉ 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

ยฉ 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?