Composite Observer

Alexey Kuznetsov
1 min readNov 9, 2015

--

Last time I wrote about the usage of the classic observer pattern instead of NSNotifications. The example showed a user agent object taking a single observer object. But NSNotificationCenter allows sending a message to many subscribers. How could we implement the same with the observer pattern?

We use composition! This is also an example of how two patterns—observer and composite—can be used together.

Notice how all objects remain immutable, stick to the single responsibility principle, are using the initializer dependency injection, and are easily testable, also without any testing frameworks.

Related Articles

--

--