-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathContents.swift
77 lines (63 loc) · 1.56 KB
/
Contents.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import Foundation
enum Engine {
case v2
case v8
case v12
}
enum Price {
case cheap
case affordable
case expensive
}
protocol Car {
var name: String { get }
var engine: Engine { get }
var price: Price { get }
}
struct Bugaggi: Car {
var name: String = "Buggagi Chevron"
var engine: Engine = .v12
var price: Price = .expensive
}
extension Bugaggi: CustomStringConvertible {
var description: String {
return "name: \(name), engine: \(engine), price: \(price)"
}
}
struct Fodr: Car {
var name: String = "Fodr Pahamareno"
var engine: Engine = .v8
var price: Price = .affordable
}
extension Fodr: CustomStringConvertible {
var description: String {
return "name: \(name), engine: \(engine), price: \(price)"
}
}
struct Truck: Car {
var name: String = "Truck Trashmobile"
var engine: Engine = .v2
var price: Price = .cheap
}
extension Truck: CustomStringConvertible {
var description: String {
return "name: \(name), engine: \(engine), price: \(price)"
}
}
class CarFactory {
func produce(price: Price) -> Car {
switch price {
case .expensive:
return Bugaggi()
case .affordable:
return Fodr()
case .cheap:
return Truck()
}
}
}
let carFactory = CarFactory()
let expensivePowerfulCar = carFactory.produce(price: .expensive)
print("expensivePowerfulCar: ", expensivePowerfulCar)
let cheapAndNotPoweful = carFactory.produce(price: .cheap)
print("cheapAndNotPoweful: ", cheapAndNotPoweful)