Skip to content

Commit cdb8ba5

Browse files
committed
feat: add factory method
1 parent 8be190e commit cdb8ba5

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

Help.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
5E023E89268364C3002465D8 /* ArrayEnumerated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E023E88268364C3002465D8 /* ArrayEnumerated.swift */; };
1111
5E023E8B2683692F002465D8 /* ArrayZip.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E023E8A2683692F002465D8 /* ArrayZip.swift */; };
1212
5E023E8F26863070002465D8 /* Dictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E023E8E26863070002465D8 /* Dictionary.swift */; };
13+
5E1F2F4927873121009446ED /* FactoryMethod.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E1F2F4827873121009446ED /* FactoryMethod.swift */; };
1314
5E2CF7C1266E20AE007BB2EB /* UnwindSegue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C0266E20AE007BB2EB /* UnwindSegue.swift */; };
1415
5E2CF7C5266E242E007BB2EB /* segue-002.png in Resources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C3266E242E007BB2EB /* segue-002.png */; };
1516
5E2CF7C6266E242E007BB2EB /* segue-001.png in Resources */ = {isa = PBXBuildFile; fileRef = 5E2CF7C4266E242E007BB2EB /* segue-001.png */; };
@@ -58,6 +59,7 @@
5859
5E023E88268364C3002465D8 /* ArrayEnumerated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayEnumerated.swift; sourceTree = "<group>"; };
5960
5E023E8A2683692F002465D8 /* ArrayZip.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayZip.swift; sourceTree = "<group>"; };
6061
5E023E8E26863070002465D8 /* Dictionary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dictionary.swift; sourceTree = "<group>"; };
62+
5E1F2F4827873121009446ED /* FactoryMethod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FactoryMethod.swift; sourceTree = "<group>"; };
6163
5E2CF7C0266E20AE007BB2EB /* UnwindSegue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnwindSegue.swift; sourceTree = "<group>"; };
6264
5E2CF7C3266E242E007BB2EB /* segue-002.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "segue-002.png"; sourceTree = "<group>"; };
6365
5E2CF7C4266E242E007BB2EB /* segue-001.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "segue-001.png"; sourceTree = "<group>"; };
@@ -115,6 +117,14 @@
115117
/* End PBXFrameworksBuildPhase section */
116118

117119
/* Begin PBXGroup section */
120+
5E1F2F4727872F9B009446ED /* DesignPatterns */ = {
121+
isa = PBXGroup;
122+
children = (
123+
5E1F2F4827873121009446ED /* FactoryMethod.swift */,
124+
);
125+
path = DesignPatterns;
126+
sourceTree = "<group>";
127+
};
118128
5E2CF7BF266E2087007BB2EB /* UIKit */ = {
119129
isa = PBXGroup;
120130
children = (
@@ -172,6 +182,7 @@
172182
5E8385872667C57600ACA770 /* WorkingCode */,
173183
5E2CF7BF266E2087007BB2EB /* UIKit */,
174184
5EAF0B7F268C5E9200D92322 /* Features */,
185+
5E1F2F4727872F9B009446ED /* DesignPatterns */,
175186
);
176187
path = Help;
177188
sourceTree = "<group>";
@@ -350,6 +361,7 @@
350361
files = (
351362
5EDE65CE26E29E0000DB71C0 /* SwipeScreen.swift in Sources */,
352363
5E8385862667BE7500ACA770 /* Int+Random.swift in Sources */,
364+
5E1F2F4927873121009446ED /* FactoryMethod.swift in Sources */,
353365
5EAC92C726BD35E700A6198C /* SaveAndLoadDataOnDevice.swift in Sources */,
354366
5E2CF7C8266E61A0007BB2EB /* PrepareSegue.swift in Sources */,
355367
5E88DEAC273D3C5800363BF6 /* String+CompareNumbers.swift in Sources */,
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//
2+
// FactoryMethod.swift
3+
// Help
4+
//
5+
// Created by Sergey Lukaschuk on 06.01.2022.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
// structure
12+
13+
// MARK: Creator
14+
final class FactoryExercises {
15+
16+
static let defaultFactory = FactoryExercises()
17+
18+
func createExercise(name: Exercises) -> Exercise {
19+
switch name {
20+
case .jumping: return Jumping()
21+
case .running: return Running()
22+
}
23+
}
24+
25+
private init() {}
26+
}
27+
28+
enum Exercises {
29+
case jumping
30+
case running
31+
}
32+
33+
// MARK: Product Interface
34+
protocol Exercise {
35+
var name: String { get }
36+
var type: String { get }
37+
38+
func start()
39+
func stop()
40+
}
41+
42+
43+
// MARK: Product A
44+
class Jumping: Exercise {
45+
46+
var name: String = "Jumping"
47+
var type: String = "Exercise for legs"
48+
49+
func start() {
50+
print("Start exercise for legs")
51+
}
52+
53+
func stop() {
54+
print("Stop exercise for legs")
55+
}
56+
}
57+
58+
// MARK: Product B
59+
class Running: Exercise {
60+
61+
var name: String = "Running"
62+
var type: String = "Exercise for running"
63+
64+
func start() {
65+
print("Start exercise for running")
66+
}
67+
68+
func stop() {
69+
print("Stop exercise for running")
70+
}
71+
}
72+
73+
74+
// MARK: Implementation
75+
class SomeViewController: UIViewController {
76+
77+
var workout: [Exercise] = []
78+
79+
override func viewDidLoad() {
80+
super.viewDidLoad()
81+
addExercise(.jumping)
82+
addExercise(.running)
83+
myWorkout()
84+
startWorkout()
85+
stopWorkout()
86+
}
87+
88+
func addExercise(_ exercise: Exercises) {
89+
let newExercise = FactoryExercises.defaultFactory.createExercise(name: exercise)
90+
workout.append(newExercise)
91+
}
92+
93+
func myWorkout() {
94+
workout.forEach { print($0.name) }
95+
}
96+
97+
func startWorkout() {
98+
workout.forEach { $0.start() }
99+
}
100+
101+
func stopWorkout() {
102+
workout.forEach { $0.stop() }
103+
}
104+
105+
}

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ In this project, I have collected various best practices and iOS development tip
5757
- [Add Home Screen Quick Actions](#add-home-screen-quick-actions)
5858

5959

60+
- [**Design Patterns**](#design-patterns)
61+
- [Factory method](factory-method)
62+
63+
64+
6065
## **Clean Code**
6166
Raising code readability in iOS development. Thanks to the application of these tips, your code will become readable, which will further ensure convenience and speed of working with it.
6267

@@ -1349,6 +1354,20 @@ private func respondToSwipeGesture(gesture: UIGestureRecognizer) {
13491354
```
13501355
13511356
1357+
1358+
## **Design Patterns**
1359+
[Book Design Patterns](https://en.wikipedia.org/wiki/Design_Patterns)
1360+
1361+
1362+
1363+
### [Factory Method](https://github.com/lgreydev/Help/blob/master/Help/DesignPatterns/FactoryMethod.swift)
1364+
[Factory Method RU](https://refactoring.guru/ru/design-patterns/factory-method)
1365+
1366+
1367+
1368+
1369+
1370+
13521371
### Add Home Screen Quick Actions
13531372
13541373
Expose commonly used functionality with static or dynamic 3D Touch Home Screen quick actions.

0 commit comments

Comments
 (0)