-
Notifications
You must be signed in to change notification settings - Fork 1
/
single_responsibilities_principle.cpp
73 lines (61 loc) · 1.69 KB
/
single_responsibilities_principle.cpp
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
/***
Acknowledagement: This is a lecture note for course https://www.udemy.com/course/patterns-cplusplus/ by
@Dmitri Nesteruk
Title: Single Responsibilities Principle
Task: Say we have a class called Journal and it has some functions. Now at some point
we may want to save the Journal to local, so we add some code called persistent code.
Here it's a bad idea to put persistence code in the class. Imagine you have hundreds
of class and each of them has a persistence code.
When your database changed, you have to go to all the hundreds class to modify them.
It's a better diea to create another class for handling all the persistence code.
This is called Separation of concerns.
Shiyu Mou
***/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/lexical_cast.hpp>
using namespace std;
struct Journal
{
string title;
vector<string> entries;
explicit Journal(const string& title)
: title{title}
{
}
void add(const string& entry);
// persistence is a separate concern
void save(const string& filename);
};
void Journal::add(const string& entry)
{
static int count = 1;
entries.push_back(boost::lexical_cast<string>(count++)
+ ": " + entry);
}
void Journal::save(const string& filename)
{
ofstream ofs(filename);
for (auto& s : entries)
ofs << s << endl;
}
struct PersistenceManager
{
static void save(const Journal& j, const string& filename)
{
ofstream ofs(filename);
for (auto& s : j.entries)
ofs << s << endl;
}
};
void main()
{
Journal journal{"Dear Diary"};
journal.add("I ate a bug");
journal.add("I cried today");
//journal.save("diary.txt");
PersistenceManager pm;
pm.save(journal, "diary.txt");
}