-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingObserver.h
More file actions
60 lines (49 loc) · 1.14 KB
/
LoggingObserver.h
File metadata and controls
60 lines (49 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef LOGGINGOBSERVER_H
#define LOGGINGOBSERVER_H
#include <iostream>
#include <fstream>
#include <list>
#include <string>
/**
* @brief Interface for classes that can produce log messages.
*/
class ILoggable {
public:
virtual ~ILoggable() = default;
virtual std::string stringToLog() const = 0;
};
/**
* @brief Abstract observer in the Observer design pattern.
*/
class Observer {
public:
virtual ~Observer() = default;
virtual void Update(ILoggable* loggable) = 0;
};
/**
* @brief Subject (observable) base class.
*/
class Subject {
private:
std::list<Observer*>* observers;
public:
Subject();
Subject(const Subject& other);
Subject& operator=(const Subject& other);
virtual ~Subject();
void Attach(Observer* o);
void Detach(Observer* o);
void Notify(ILoggable* loggable) const;
};
/**
* @brief Concrete observer that writes log entries to gamelog.txt.
*/
class LogObserver : public Observer {
private:
std::ofstream logfile;
public:
LogObserver();
~LogObserver();
void Update(ILoggable* loggable) override;
};
#endif