r/iOSProgramming • • Sep 11 '22

Article Observer Pattern: Protocol Oriented Design Pattern

The observer pattern is a behavior design pattern where a type maintains a list of objects, called observers, and notifies them automatically on changes to a particular state. In this post, we will be using Swift’s built in property observers but we will show how to decouple the observer from the property it is observing using protocol-oriented techniques.

https://www.mastering-swift.com/post/observer-pattern-protocol-oriented-design-pattern

1 Upvotes

6 comments sorted by

View all comments

6

u/VuPooh Sep 12 '22

Why not just use combine?

2

u/Pop_Swift_Dev Sep 12 '22

Every framework has their own limitations, having the knowledge to be able to roll your own when necessary is definitely not a bad thing, is it?

4

u/rhysmorgan Sep 12 '22

Having the knowledge to roll your own is definitely good, but nobody should be writing their own observation framework when RxSwift and Combine exist.

Any holes, any limitations that exist in Combine's implementation can almost certainly be patched by writing your own Publisher/Subscriber/Subscription, not by throwing the whole thing away and re-inventing the wheel. Chances are, your own implementation will be slower, won't handle backpressure, won't handle the edge cases Combine does, and it certainly won't integrate with existing code in the same way Combine can.

So the true answer to "Why not just use combine?" is "Absolutely, you should 100% be using Combine (or RxSwift if targeting < iOS 13), but this is an example implementation of the design pattern."

2

u/Pop_Swift_Dev Sep 12 '22

Very well put, thank you. Agreed

I do have to say though, I can't count the number of times I have said, I should never need to_____ (or something similar) and then the universe comes back and says "Not so fast". Talking about more than just programming :).