3939Defines the protocol for objects that would like to be notified of changes to the bag products.
4040
4141``` swift
42- protocol BagObserver : AnyObject {
43- func bagUpdatedWithProducts (_ products : [Product] )
42+ protocol Observer : AnyObject {
43+ func notificationWithObject (_ object : Any )
4444}
4545```
4646
@@ -55,35 +55,39 @@ protocol BagObserver: AnyObject {
5555- We attach the observer to the subject in the initializer, and detach it in the deinitializer.
5656
5757``` swift
58- class BagListViewModel : BagObserver {
59- private let notifier: BagNotifier
60- private ( set ) var products: [Product] = []
58+ class BagListViewModel : Observer {
59+ var products: [Product] = []
60+ private let notifier: Notifier
6161
62- init (notifier : BagNotifier ) {
62+ init (notifier : Notifier ) {
6363 self .notifier = notifier
6464 self .notifier .attachObserver (self )
6565 }
6666
67- func bagUpdatedWithProducts (_ products : [Product]) {
68- self .products = products
67+ func notificationWithObject (_ object : Any ) {
68+ if let products = object as? [Product] {
69+ self .products = products
70+ }
6971 }
7072
7173 deinit {
7274 notifier.detachObserver (self )
7375 }
7476}
7577
76- class BagIconViewModel : BagObserver {
77- private let notifier: BagNotifier
78- private ( set ) var badgeCount: Int = 0
78+ class BagIconViewModel : Observer {
79+ var badgeCount: Int = 0
80+ private let notifier: Notifier
7981
80- init (notifier : BagNotifier ) {
82+ init (notifier : Notifier ) {
8183 self .notifier = notifier
8284 self .notifier .attachObserver (self )
8385 }
8486
85- func bagUpdatedWithProducts (_ products : [Product]) {
86- self .badgeCount = products.count
87+ func notificationWithObject (_ object : Any ) {
88+ if let products = object as? [Product] {
89+ self .badgeCount = products.count
90+ }
8791 }
8892
8993 deinit {
@@ -97,9 +101,9 @@ class BagIconViewModel: BagObserver {
97101Defines the protocol for managing observers.
98102
99103``` swift
100- protocol BagNotifier {
101- func attachObserver (_ observer : BagObserver )
102- func detachObserver (_ observer : BagObserver )
104+ protocol Notifier {
105+ func attachObserver (_ observer : Observer )
106+ func detachObserver (_ observer : Observer )
103107 func notify ()
104108}
105109```
@@ -113,24 +117,24 @@ protocol BagNotifier {
113117- Sends a notification to its observers when its state changes.
114118
115119``` swift
116- class WebSocketBagNotifier : BagNotifier {
117- private var observers: [BagObserver ] = []
120+ class WebSocketBagNotifier : Notifier {
121+ private var observers: [Observer ] = []
118122 private var products: [Product] = []
119123
120- func attachObserver (_ observer : BagObserver ) {
124+ func attachObserver (_ observer : Observer ) {
121125 observers.append (observer)
122126 }
123127
124- func detachObserver (_ observer : BagObserver ) {
128+ func detachObserver (_ observer : Observer ) {
125129 observers.removeAll { $0 === observer }
126130 }
127131
128132 func notify () {
129- observers.forEach { $0 .bagUpdatedWithProducts (self .products ) }
133+ observers.forEach { $0 .notificationWithObject (self .products ) }
130134 }
131135
132- func testReceivingUpdatedBagProducts (_ product : [ Product] ) {
133- self .products = product
136+ func testNotificationAfterAddingProduct (_ product : Product) {
137+ self .products . append ( product)
134138 notify ()
135139 }
136140}
@@ -140,7 +144,6 @@ class WebSocketBagNotifier: BagNotifier {
140144
141145``` swift
142146struct Product {
143- let id: String
144147 let name: String
145148 let price: Double
146149}
@@ -149,13 +152,17 @@ let notifier = WebSocketBagNotifier()
149152let bagListViewModel = BagListViewModel (notifier : notifier)
150153let bagIconViewModel = BagIconViewModel (notifier : notifier)
151154
155+ // Default values
156+
152157print (bagListViewModel.products ) // []
153158print (bagIconViewModel.badgeCount ) // 0
154159
155- notifier.testReceivingUpdatedBagProducts ([
156- Product (id : " 1" , name : " iPad Pro" , price : 999.99 )
157- ])
160+ notifier.testNotificationAfterAddingProduct (
161+ Product (name : " iPad Pro" , price : 999.99 )
162+ )
163+
164+ // Values after product added to the bag
158165
159- print (bagListViewModel.products ) // [Product(id: "1", name: "iPad Pro", price: 999.99)]
166+ print (bagListViewModel.products ) // [Product(name: "iPad Pro", price: 999.99)]
160167print (bagIconViewModel.badgeCount ) // 1
161168```
0 commit comments