-
Notifications
You must be signed in to change notification settings - Fork 470
Description
Summary
The current Observer example lets the same Subscriber subscribe to multiple Channel instances, but Subscriber::update() always uses only the first Channel* passed at construction time. This causes incorrect behavior when a single subscriber is attached to multiple channels.
Steps to Reproduce
Channel ch1("CoderArmy");
Channel ch2("TechArmy");
Subscriber s1("Varun", &ch1); // stores ch1 internally
ch1.subscribe(&s1);
ch2.subscribe(&s1);
ch1.uploadVideo("Observer Pattern Tutorial"); // OK
ch2.uploadVideo("Decorator Pattern Tutorial"); // ❌ prints video from ch1 instead of ch2.Actual Behavior
Varun receives a message for ch2, but the message text uses ch1's latestVideo.
Expected Behavior
When a Channel notifies subscribers, the Subscriber should know which channel triggered the notification and display the correct video's title.
Suggested Fix
Change the update() method to accept a pointer/reference to the notifying Channel. For example:
-class ISubscriber {
-public:
- virtual void update() = 0;
+class ISubscriber {
+public:
+ virtual void update(IChannel* source) = 0;
And in Channel::notifySubscribers:
- for (ISubscriber* sub : snapshot) {
- if (sub) sub->update();
- }
+ for (ISubscriber* sub : snapshot) {
+ if (sub) sub->update(this);
+ }
Then Subscriber::update() can use source->getVideoData() instead of always using its stored Channel*. This allows the same Subscriber instance to receive updates from multiple channels correctly.