Repository navigation
Expand file tree
/
Copy pathopen_closed_principle.cpp
More file actions
executable file
·130 lines (96 loc) · 2.91 KB
/
Copy pathopen_closed_principle.cpp
File metadata and controls
executable file
·130 lines (96 loc) · 2.91 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
/*
https://www.tomdalling.com/blog/software-design/solid-class-design-the-open-closed-principle/
You should be able to extend a classes behavior, without modifying it.
//XMLConverter
class XMLConverter {
public:
String convertDocumentToXML(Document doc);
}
//BinaryConverter
class BinaryConverter {
public:
Data convertDocumentToBinary(Document doc);
}
//DocumentExporter
enum ConverterType {
XMLConverterType,
BinaryConverterType
};
class DocumentExporter {
private:
URL _runSaveDialog();
void _showSuccessDialog;
ConverterType _converterType;
public:
void setConverterType(ConverterType converterType);
void exportDocument(Document doc);
};
void DocumentExporter::exportDocument(Document doc)
{
URL fileURL = _runSaveDialog();
switch(_converterType){
case XMLConverterType:{
XMLConverter xmlConverter;
String xmlFileContent = xmlConverter.convertDocumentToXML(doc);
xmlFileContent.writeToURL(fileURL);
break;
}
case BinaryConverterType:{
BinaryConverter binaryConverter;
Data binaryFileContent = binaryConverter.convertDocumentToBinary(doc);
binaryFileContent.writeToURL(fileURL);
break;
}
default:
LogError("Unrecognised converter type");
return;
}
_showSuccessDialog();
}
The DocumentExporter class is not closed to change. Every time a new export format must be supported, the DocumentExporter class must be modified.
"𝙀𝙣𝙪𝙢𝙨 𝙖𝙣𝙙 𝙨𝙬𝙞𝙩𝙘𝙝 𝙨𝙩𝙖𝙩𝙚𝙢𝙚𝙣𝙩𝙨" are strong indicators that a class is not closed to changes.
he way to close DocumentExporter to changes, in this case, is to make an abstract base class for all the converters. Then, the converter can be supplied
to DocumentExporter via a technique called dependency injection. The solution is as follows:
class Converter {
public:
virtual Data convertDocumentToData(Document doc) = 0;
};
//XMLConverter
class XMLConverter : public Converter {
public:
Data convertDocumentToData(Document doc);
};
Data XMLConverter::convertDocumentToData(Document doc)
{
//convert to xml here
}
//BinaryConverter
class BinaryConverter : public Converter {
public:
Data convertDocumentToData(Document doc);
};
Data BinaryConverter::convertDocumentToData(Document doc)
{
//convert to binary here
}
//DocumentExporter
class DocumentExporter {
private:
URL _runSaveDialog();
void _showSuccessDialog;
Converter* _converter;
public:
void setConverter(Converter* converter); //Here is the dependency injection function
void exportDocument(Document doc);
};
void DocumentExporter::exportDocument(Document doc)
{
URL fileURL = _runSaveDialog();
Data fileContent = _converter.convertDocumentToData(doc);
fileContent.writeToURL(fileURL);
_showSuccessDialog();
}
*/
int main()
{
}