This is a space that holds a collection of my personal work and ideas
Behavioral Patterns - Observer Pattern Exercise
Posted on 06/20/2018
The Observer Pattern is widely used to solve one-to-many object state changes notification problems. The pub/sub model uses the Observer Pattern as the base pattern. It is also used in the event-driven systems. In this exercise, we will create a naive implementation of the pub/sub model to understand the components of the Observer Pattern.
Use the Observer Pattern to create a naive implementation of the pub/sub model.
Components:
Observer (The interface that defines the callback method signature or the update method when it is notified.) - Observer
Subject (The interface that observers listen to) - Subject
AbstractSubscriber (The abstract class that attach the observer to a subject) - AbstractSubscriber
AbstractTopic (The abstract class that keep track of a list of observers and provides observer registration implementations) - AbstractTopic
TextMessageSubscriber (The concrete class that represents a subscriber that will print out the message that is published) - TextMessageSubscriber
TextMessageTopic (The concrete class that represents a topic that defines the message publishing implementation) - TextMessageTopic
Client (The class that creates topics and subscribers, registers and unregisters them, and publishes messages to topics) - App
Tip
A Queue could be used to keep track of published message to the Topic. After notifying all subscribers about the head message of the queue, the message should be dequeued.