@@ -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
14311422class 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