Skip to content

Commit 640257f

Browse files
committed
docs: fix
1 parent 4b6bbab commit 640257f

2 files changed

Lines changed: 63 additions & 102 deletions

File tree

Help/DesignPatterns/FactoryMethod.swift

Lines changed: 31 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,95 +10,75 @@ import UIKit
1010

1111
// structure Factory Method
1212
// MARK: Creator
13-
class FactoryExercises {
13+
class FactoryProducts {
1414

15-
static let defaultFactory = FactoryExercises()
15+
static let defaultFactory = FactoryProducts()
1616

17-
func createExercise(name: Exercises) -> Exercise {
18-
switch name {
19-
case .jumping: return Jumping()
20-
case .running: return Running()
17+
func createProduct(_ product: Products) -> Product_ {
18+
switch product {
19+
case .productA: return ConcreteProductA()
20+
case .productB: return ConcreteProductB()
2121
}
2222
}
2323

2424
private init() {}
2525
}
2626

27-
enum Exercises {
28-
case jumping
29-
case running
27+
enum Products {
28+
case productA
29+
case productB
3030
}
3131

3232
// MARK: Product Interface
33-
protocol Exercise {
33+
protocol Product_ {
34+
var id: String { get }
3435
var name: String { get }
35-
var type: String { get }
3636

37-
func start()
38-
func stop()
37+
func printProduct()
3938
}
4039

4140

4241
// MARK: Product A
43-
class Jumping: Exercise {
42+
class ConcreteProductA: Product_ {
4443

45-
var name: String = "Jumping"
46-
var type: String = "Exercise for legs"
44+
var name: String = "Product A"
45+
var id: String = "1"
4746

48-
func start() {
49-
print("Start exercise for legs")
50-
}
51-
52-
func stop() {
53-
print("Stop exercise for legs")
47+
func printProduct() {
48+
print("id: \(id), name: \(name)")
5449
}
5550
}
5651

5752
// MARK: Product B
58-
class Running: Exercise {
53+
class ConcreteProductB: Product_ {
5954

60-
var name: String = "Running"
61-
var type: String = "Exercise for running"
55+
var name: String = "Product B"
56+
var id: String = "2"
6257

63-
func start() {
64-
print("Start exercise for running")
65-
}
66-
67-
func stop() {
68-
print("Stop exercise for running")
58+
func printProduct() {
59+
print("id: \(id), name: \(name)")
6960
}
7061
}
7162

7263

7364
// MARK: Implementation
7465
class SomeViewController: UIViewController {
7566

76-
var workout: [Exercise] = []
67+
var products: [Product_] = []
7768

7869
override func viewDidLoad() {
7970
super.viewDidLoad()
80-
addExercise(.jumping)
81-
addExercise(.running)
82-
myWorkout()
83-
startWorkout()
84-
stopWorkout()
71+
addProduct(.productA)
72+
addProduct(.productB)
73+
allProducts()
8574
}
8675

87-
func addExercise(_ exercise: Exercises) {
88-
let newExercise = FactoryExercises.defaultFactory.createExercise(name: exercise)
89-
workout.append(newExercise)
76+
func addProduct(_ product: Products) {
77+
let newProduct = FactoryProducts.defaultFactory.createProduct(product)
78+
products.append(newProduct)
9079
}
9180

92-
func myWorkout() {
93-
workout.forEach { print($0.name) }
81+
func allProducts() {
82+
products.forEach { $0.printProduct() }
9483
}
95-
96-
func startWorkout() {
97-
workout.forEach { $0.start() }
98-
}
99-
100-
func stopWorkout() {
101-
workout.forEach { $0.stop() }
102-
}
103-
10484
}

README.md

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,99 +1367,80 @@ private func respondToSwipeGesture(gesture: UIGestureRecognizer) {
13671367
13681368
// structure Factory Method
13691369
// MARK: Creator
1370-
class FactoryExercises {
1370+
class FactoryProducts {
13711371
1372-
static let defaultFactory = FactoryExercises()
1372+
static let defaultFactory = FactoryProducts()
13731373
1374-
func createExercise(name: Exercises) -> Exercise {
1375-
switch name {
1376-
case .jumping: return Jumping()
1377-
case .running: return Running()
1374+
func createProduct(_ product: Products) -> Product {
1375+
switch product {
1376+
case .productA: return ConcreteProductA()
1377+
case .productB: return ConcreteProductB()
13781378
}
13791379
}
13801380
13811381
private init() {}
13821382
}
13831383
1384-
enum Exercises {
1385-
case jumping
1386-
case running
1384+
enum Products {
1385+
case productA
1386+
case productB
13871387
}
13881388
13891389
// MARK: Product Interface
1390-
protocol Exercise {
1390+
protocol Product {
1391+
var id: String { get }
13911392
var name: String { get }
1392-
var type: String { get }
13931393
1394-
func start()
1395-
func stop()
1394+
func printProduct()
13961395
}
13971396
13981397
13991398
// MARK: Product A
1400-
class Jumping: Exercise {
1399+
class ConcreteProductA: Product {
14011400
1402-
var name: String = "Jumping"
1403-
var type: String = "Exercise for legs"
1401+
var name: String = "Product A"
1402+
var id: String = "1"
14041403
1405-
func start() {
1406-
print("Start exercise for legs")
1407-
}
1408-
1409-
func stop() {
1410-
print("Stop exercise for legs")
1404+
func printProduct() {
1405+
print("id: \(id), name: \(name)")
14111406
}
14121407
}
14131408
14141409
// MARK: Product B
1415-
class Running: Exercise {
1410+
class ConcreteProductB: Product {
14161411
1417-
var name: String = "Running"
1418-
var type: String = "Exercise for running"
1412+
var name: String = "Product B"
1413+
var id: String = "2"
14191414
1420-
func start() {
1421-
print("Start exercise for running")
1422-
}
1423-
1424-
func stop() {
1425-
print("Stop exercise for running")
1415+
func printProduct() {
1416+
print("id: \(id), name: \(name)")
14261417
}
14271418
}
14281419
14291420
14301421
// MARK: Implementation
14311422
class SomeViewController: UIViewController {
14321423
1433-
var workout: [Exercise] = []
1424+
var products: [Product] = []
14341425
14351426
override func viewDidLoad() {
14361427
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)
1428+
addProduct(.productA)
1429+
addProduct(.productB)
1430+
allProducts()
14471431
}
14481432
1449-
func myWorkout() {
1450-
workout.forEach { print($0.name) }
1451-
}
1452-
1453-
func startWorkout() {
1454-
workout.forEach { $0.start() }
1433+
func addProduct(_ product: Products) {
1434+
let newProduct = FactoryProducts.defaultFactory.createProduct(product)
1435+
products.append(newProduct)
14551436
}
14561437
1457-
func stopWorkout() {
1458-
workout.forEach { $0.stop() }
1438+
func allProducts() {
1439+
products.forEach { $0.printProduct() }
14591440
}
1460-
14611441
}
14621442
1443+
14631444
```
14641445
14651446

0 commit comments

Comments
 (0)