Skip to content

Commit ccbf49e

Browse files
committed
factory pattern
1 parent 4e33a93 commit ccbf49e

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import UIKit
2+
3+
/**
4+
Thank you for taking time and watching the vide, if you have questions then please feel free to ask them via comments or via email.
5+
6+
If you are new to the channel, I request you to please subscribe to the channel as the more people subscribe the more it motivates me to bring quality content for free.
7+
8+
Do share the channel with your iOS group and do let me know what I can do better to improve my content or any video requests you can think of
9+
10+
Thank you once again, have a nice day
11+
12+
Ravi a.k.a codecat15
13+
14+
*/
15+
16+
17+
// Model
18+
struct Report {
19+
20+
let id : UUID
21+
let year: Int
22+
let total: Double
23+
24+
static func generateDummyReport() -> Array<Report> {
25+
26+
return [Report(id: UUID(), year: Int.random(in: 2015..<2022), total: Double.random(in: 2000..<5000)),
27+
Report(id: UUID(), year: Int.random(in: 2015..<2022), total: Double.random(in: 2000..<5000))]
28+
}
29+
}
30+
31+
protocol ReportProtocol {
32+
func getReport() -> Array<Report>
33+
}
34+
35+
class TaxReport : ReportProtocol {
36+
37+
func getReport() -> Array<Report> {
38+
print("fetching tax report")
39+
return Report.generateDummyReport()
40+
}
41+
}
42+
43+
class ProfitReport : ReportProtocol {
44+
45+
func getReport() -> Array<Report> {
46+
47+
// code for calculating profit report
48+
print("fetching profit report")
49+
return Report.generateDummyReport()
50+
}
51+
}
52+
53+
class FinanceReport : ReportProtocol {
54+
func getReport() -> Array<Report> {
55+
56+
// code for calculating profit report
57+
print("fetching finance report")
58+
return Report.generateDummyReport()
59+
}
60+
}
61+
62+
enum ReportType {
63+
case tax
64+
case profit
65+
case finance
66+
}
67+
68+
class ReportFactory {
69+
static func create(type: ReportType) -> ReportProtocol {
70+
71+
switch type {
72+
case .tax:
73+
return TaxReport()
74+
case .profit:
75+
return ProfitReport()
76+
case .finance:
77+
return FinanceReport()
78+
}
79+
}
80+
}
81+
82+
class ReportViewModel {
83+
private let report : ReportProtocol
84+
85+
init(_report : ReportProtocol) {
86+
report = _report
87+
}
88+
func getReport() -> Array<Report> {
89+
return report.getReport()
90+
}
91+
}
92+
93+
let reportViewModel = ReportViewModel(_report: ReportFactory.create(type: .finance))
94+
reportViewModel.getReport()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Patterns/Factory pattern/MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)