Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observer #19

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Observer #19

wants to merge 6 commits into from

Conversation

xsdc
Copy link
Owner

@xsdc xsdc commented Nov 14, 2024

No description provided.


## Problem statement

- We would like to keep the bag badge count up to date.
- The observer pattern allows us to notify the bag icon view with the updated badge count when a product is added or removed.
- Each Apple Store customer's bag contains an array of products, which are stored on the server.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bag? Do you mean cart?

- The observer pattern allows us to notify the bag icon view with the updated badge count when a product is added or removed.
- Each Apple Store customer's bag contains an array of products, which are stored on the server.

- When on the Apple Store bag list view, we would like to keep the array of products up to date, even if they are added or removed from another device.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cart view. List is redundant in this context.


- When on the Apple Store bag list view, we would like to keep the array of products up to date, even if they are added or removed from another device.

- In the top navigation, we would also like the bag icon view badge count to be in sync with the bag product count.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search for all instances of "bag"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"cart icon badge count"
"view" is redundant.


- Both the bag icon view and bag list view need to be updated when products are added or removed.

- We would like to avoid having to check for changes in each individual view.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make this problem statement stronger? How about: "Without implementing the observer patter views would need to fetch data in order to stay up to date, and this may result in unnecessary fetching, or the views not staying in sync with the model.


## Domain application
- This allows for a decoupled design where there is a clear separation of concerns between the subject and the observers, conforming to the single responsibility principle.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's true that NOT conforming to the observer pattern would mean we are violating SRP. Furthermore, using the Observer pattern doesn't guarantee it either. Suggest removing ", conforming to the single responsibility principle"

func attach(observer: BagObserver)
func detach(observer: BagObserver)
func notify()
protocol Observer: AnyObject {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, why the need for AnyObject?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was needed for the observer removal method, but since it's not used in the example, leaving a comment instead


```swift
protocol BagObserver {
func updateBagCount(_ count: Int)
procotol ObserverManaging {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling: protocol not procotol


func attach(observer: BagObserver) {
observers.append(observer)
func testNotificationAfterAddingProduct(_ product: Product) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this method called "test"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, you added it so your example works. I get it, but I'm not a fan of adding code like this to examples. It's confusing to the reader.

Copy link
Collaborator

@ryanbrear ryanbrear left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix these issues then merge.

Comment on lines +126 to 128
func addProduct(_ product: Product) {
self.products.append(product)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a product is added surely you want to notify observers?? There should be a call to notify in this method, and notify should be private.

notifier.addProduct(
Product(name: "iPad Pro", price: 999.99)
)
notifier.notify()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need to call notify on the notifier. The class should be set up to handle notifications when its data changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants