Skip to content

Commit f8a940a

Browse files
committed
docs: fix
1 parent cdb8ba5 commit f8a940a

2 files changed

Lines changed: 92 additions & 7 deletions

File tree

Help/DesignPatterns/FactoryMethod.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
import Foundation
99
import UIKit
1010

11-
// structure
12-
11+
// structure Factory Method
1312
// MARK: Creator
1413
final class FactoryExercises {
1514

README.md

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ In this project, I have collected various best practices and iOS development tip
5858

5959

6060
- [**Design Patterns**](#design-patterns)
61-
- [Factory method](factory-method)
61+
- [Factory Method](#factory-method)
6262

6363

6464

@@ -1363,18 +1363,104 @@ private func respondToSwipeGesture(gesture: UIGestureRecognizer) {
13631363
### [Factory Method](https://github.com/lgreydev/Help/blob/master/Help/DesignPatterns/FactoryMethod.swift)
13641364
[Factory Method RU](https://refactoring.guru/ru/design-patterns/factory-method)
13651365
1366+
``` swift
13661367
1368+
// structure Factory Method
1369+
// MARK: Creator
1370+
final class FactoryExercises {
1371+
1372+
static let defaultFactory = FactoryExercises()
1373+
1374+
func createExercise(name: Exercises) -> Exercise {
1375+
switch name {
1376+
case .jumping: return Jumping()
1377+
case .running: return Running()
1378+
}
1379+
}
1380+
1381+
private init() {}
1382+
}
13671383
1384+
enum Exercises {
1385+
case jumping
1386+
case running
1387+
}
13681388
1389+
// MARK: Product Interface
1390+
protocol Exercise {
1391+
var name: String { get }
1392+
var type: String { get }
1393+
1394+
func start()
1395+
func stop()
1396+
}
13691397
13701398
1371-
### Add Home Screen Quick Actions
1399+
// MARK: Product A
1400+
class Jumping: Exercise {
1401+
1402+
var name: String = "Jumping"
1403+
var type: String = "Exercise for legs"
1404+
1405+
func start() {
1406+
print("Start exercise for legs")
1407+
}
1408+
1409+
func stop() {
1410+
print("Stop exercise for legs")
1411+
}
1412+
}
13721413
1373-
Expose commonly used functionality with static or dynamic 3D Touch Home Screen quick actions.
1414+
// MARK: Product B
1415+
class Running: Exercise {
1416+
1417+
var name: String = "Running"
1418+
var type: String = "Exercise for running"
1419+
1420+
func start() {
1421+
print("Start exercise for running")
1422+
}
1423+
1424+
func stop() {
1425+
print("Stop exercise for running")
1426+
}
1427+
}
13741428
1375-
[Documentation](https://developer.apple.com/documentation/uikit/menus_and_shortcuts/add_home_screen_quick_actions)
13761429
1377-
<img src="https://github.com/lgreydev/Help/blob/master/Screenshots/home-screen-quick-actions.jpeg" width="600">
1430+
// MARK: Implementation
1431+
class SomeViewController: UIViewController {
1432+
1433+
var workout: [Exercise] = []
1434+
1435+
override func viewDidLoad() {
1436+
super.viewDidLoad()
1437+
addExercise(.jumping)
1438+
addExercise(.running)
1439+
myWorkout()
1440+
startWorkout()
1441+
stopWorkout()
1442+
}
1443+
1444+
func addExercise(_ exercise: Exercises) {
1445+
let newExercise = FactoryExercises.defaultFactory.createExercise(name: exercise)
1446+
workout.append(newExercise)
1447+
}
1448+
1449+
func myWorkout() {
1450+
workout.forEach { print($0.name) }
1451+
}
1452+
1453+
func startWorkout() {
1454+
workout.forEach { $0.start() }
1455+
}
1456+
1457+
func stopWorkout() {
1458+
workout.forEach { $0.stop() }
1459+
}
1460+
1461+
}
1462+
1463+
```
13781464
13791465
13801466
### 🛡️ License

0 commit comments

Comments
 (0)