Observer Header
-
namespace observer_pattern
Namespace for the Observer pattern.
-
class ConcreteObserver : public observer_pattern::Observer
- #include <observer.hpp>
Concrete implementation of an Observer.
Subclassed by TestObserver
Public Functions
-
inline ConcreteObserver(const std::string &name, std::shared_ptr<Subject> subject)
Constructor for ConcreteObserver.
- Parameters:
name – The name of the observer.
subject – The subject to observe.
-
inline std::string getName() const
Get the name of the observer.
- Returns:
The name of the observer.
-
inline virtual void update() override
Updates the observer when notified by the subject.
Private Members
-
std::string name
The name of the observer.
-
std::shared_ptr<Subject> subject
The subject this observer is observing.
-
inline ConcreteObserver(const std::string &name, std::shared_ptr<Subject> subject)
-
class ConcreteSubject : public observer_pattern::Subject
- #include <observer.hpp>
Concrete implementation of a Subject.
Public Functions
-
inline virtual void attach(std::shared_ptr<Observer> observer) override
Attach an observer to the subject.
- Parameters:
observer – The observer to attach.
-
inline virtual void detach(std::shared_ptr<Observer> observer) override
Detach an observer from the subject.
- Parameters:
observer – The observer to detach.
-
inline virtual void notify() override
Notify all attached observers of a change.
-
inline virtual std::string getState() const override
Get the current state of the subject.
- Returns:
The current state as a string.
-
inline virtual void setState(const std::string &newState) override
Set the state of the subject and notify observers.
- Parameters:
newState – The new state of the subject.
Private Members
-
std::vector<std::shared_ptr<Observer>> observers
List of attached observers.
-
std::string state
The current state of the subject.
-
inline virtual void attach(std::shared_ptr<Observer> observer) override
-
class Observer
- #include <observer.hpp>
Abstract base class representing an observer in the Observer pattern.
Subclassed by observer_pattern::ConcreteObserver
Public Functions
-
virtual ~Observer() = default
Virtual destructor for cleanup of derived classes.
-
virtual void update() = 0
Called to notify the observer of changes in the subject.
-
virtual ~Observer() = default
-
class Subject
- #include <observer.hpp>
Abstract base class representing a subject in the Observer pattern.
Subclassed by observer_pattern::ConcreteSubject
Public Functions
-
virtual ~Subject() = default
Virtual destructor for cleanup of derived classes.
-
virtual void attach(std::shared_ptr<Observer> observer) = 0
Attach an observer to the subject.
- Parameters:
observer – The observer to attach.
-
virtual void detach(std::shared_ptr<Observer> observer) = 0
Detach an observer from the subject.
- Parameters:
observer – The observer to detach.
-
virtual void notify() = 0
Notify all attached observers of a change.
-
virtual std::string getState() const = 0
Get the state of the subject.
- Returns:
The current state of the subject as a string.
-
virtual void setState(const std::string &state) = 0
Set the state of the subject.
- Parameters:
state – The new state of the subject.
-
virtual ~Subject() = default
-
class ConcreteObserver : public observer_pattern::Observer