-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prototype.swift
53 lines (36 loc) · 1.14 KB
/
Prototype.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// Prototype.swift
// Design Patterns
//
// Created by Samuel Esposito on 27/08/2019.
// Copyright © 2019 Samuel Esposito. All rights reserved.
//
import Foundation
/*Prototype design pattern is used to create a clones from existing objects, the clone and the original objects won't have a reference relationship between each other*/
/* STEP 1: define a Cloneable protocol and instantiate an initializers that will allow us to copy */
protocol Cloneable{
init(copyFrom target: Self)
}
/* STEP 2: our class will inherit from our protocol, the required init will be the clone function */
class Car: Cloneable{
var company: String
var model: String
init(company: String, model: String) {
self.company = company
self.model = model
}
required init(copyFrom other: Car) {
self.company = other.company
self.model = other.model
}
}
func main(){
let i8cabrio = Car(company: "BMW", model: "i8 Cabrio")
let series4 = Car(copyFrom: i8cabrio)
print(i8cabrio.model)
print(series4.model)
series4.model = "series 4"
print(i8cabrio.model)
print(series4.model)
}
main()