-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingleResponsibility.cpp
More file actions
140 lines (124 loc) · 4.37 KB
/
SingleResponsibility.cpp
File metadata and controls
140 lines (124 loc) · 4.37 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* @cite Single Responsibility princliple states that each entity (class, function, structs, etc.) in the code should have one and only one concern or responsibilty.
* Aims at increasing modularity by making each entity as independent as possible.
*
* @brief Single Responsibilty principle exemplified by creating a journal entity that manages entires inside a journal.
* What happens when we need to add the saving to physical storage functionality to this entity?
* What if that functionality requires modification in the future (let's say from physical storage to a database)?
*/
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
/**
* @brief General Journal class that has only one concern of managing entries.
*/
class Journal
{
protected:
std::string title;
std::vector<std::string> entries;
public:
Journal(const std::string &title) : title(title) {}
void add_entry(std::string entry)
{
entries.push_back(
std::to_string(entries.size() + 1) + ": " + entry);
}
/**
* @brief saves the journal and its entries to physical storage
*
* @warning
* ! save_journal Function adds another concern to the Journal Class that has to be taken care of when extending its functionality to children classes.
*
* ! What if the method of storage was changed? Essentially the every extension of Journal will have to be changed to accomodate the new functionality
*/
void save_journal(std::string filename)
{
std::ofstream ofs(filename);
ofs << title << std::endl;
for (auto entry : entries)
{
ofs << entry << std::endl;
}
}
friend class PersistenceManager;
};
/**
* @brief Specialized version of Journal class that also stores the name of the person adding the entry as well as the company name.
*/
class WorkJournal : public Journal
{
std::string companyName;
public:
WorkJournal(std::string title, std::string companyName) : Journal(title), companyName(companyName) {}
void add_entry(std::string entry, std::string author)
{
entries.push_back(
std::to_string(entries.size() + 1) + ": " + entry + " - " + author);
}
/**
* @brief save_journal saves the work journals to the physical storage along with the company name.
*
* @warning
* ! Whenever the implementation of saving mechanism changes this will have to be changed as well as it is an extension of Journal
*/
void save_journal(std::string filename)
{
std::ofstream ofs(filename);
ofs << title << " - " << companyName << std::endl;
for (auto entry : entries)
{
ofs << entry << std::endl;
}
}
friend class PersistenceManager;
};
/**
* @brief Persistence Manager class which is only concerned with saving the journals to physical storage.
* @details
* * Only this class will have to be changed if the implementation of saving mechanism changes.
*/
class PersistenceManager
{
public:
/**
* @brief saves the journal and its entries to physical storage
*/
static void save_journal(Journal &journal, std::string filename)
{
std::ofstream ofs(filename);
ofs << journal.title << std::endl;
for (auto entry : journal.entries)
{
ofs << entry << std::endl;
}
}
/**
* @brief save_journal saves the work journals to the physical storage along with the company name.
*/
static void save_journal(WorkJournal &journal, std::string filename)
{
std::ofstream ofs(filename);
ofs << journal.title << " - " << journal.companyName << std::endl;
for (auto entry : journal.entries)
{
ofs << entry << std::endl;
}
}
};
int main()
{
Journal diary("Dear Diary");
diary.add_entry("I started learning Design Patterns.");
diary.add_entry("Single Responsibilty principle seems gay.");
diary.save_journal("dear_diary.txt");
PersistenceManager::save_journal(diary, "dear_diary.txt");
WorkJournal notes("Work Notes", "ABC inc.");
notes.add_entry("Perform Sanity Checks on Database.", "George");
notes.add_entry("Tell Joe to learn Design Pattern.", "Helen");
notes.save_journal("work_notes.txt");
PersistenceManager::save_journal(notes, "work_notes.txt");
std::cout << "Successfully Written to Files.";
return 0;
}