-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathISP.cpp
More file actions
152 lines (118 loc) · 3.47 KB
/
ISP.cpp
File metadata and controls
152 lines (118 loc) · 3.47 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
141
142
143
144
145
146
147
148
149
150
151
152
// ===========================================================================
// ISP.cpp // Interface Segregation Principle
// ===========================================================================
#include <iostream>
#include <string>
#include <cassert>
namespace InterfaceSegregationPrinciple01
{
class Document {};
class IMachine {
public:
virtual void print(Document& doc) = 0;
virtual void fax(Document& doc) = 0;
virtual void scan(Document& doc) = 0;
};
// ok
class MultiFunctionPrinter : public IMachine {
public:
void print(Document& doc) override {
// do printing ...
}
void fax(Document& doc) override {
// do faxing ...
}
void scan(Document& doc) override {
// do scanning ...
}
};
// Not ok
class Scanner : public IMachine {
public:
void print(Document& doc) override {
throw std::runtime_error("printing not supported");
}
void fax(Document& doc) override {
throw std::runtime_error("faxing not supported");
}
void scan(Document& doc) override {
// do scanning ...
}
};
}
namespace InterfaceSegregationPrinciple02
{
class Document {};
/* ---------------------- Interfaces ------------------- */
class IPrinter {
public:
virtual void print(Document& doc) = 0;
};
class IScanner {
public:
virtual void scan(Document& doc) = 0;
};
class IFaxMachine {
public:
virtual void fax(Document& doc) = 0;
};
class IMachine : public IPrinter, public IScanner, public IFaxMachine {};
/* ---------------------- Implementation ------------------- */
class Printer : public IPrinter {
public:
void print(Document& doc) override { };
};
class Scanner : public IScanner {
public:
void scan(Document& doc) override { };
};
class FaxMachine : public IFaxMachine {
public:
void fax(Document& doc) override { };
};
class Machine : public IMachine {
public:
IPrinter& m_printer;
IScanner& m_scanner;
IFaxMachine& m_faxmachine;
Machine(IPrinter& printer, IScanner& scanner, IFaxMachine& faxmachine)
: m_printer{ printer }, m_scanner{ scanner }, m_faxmachine{ faxmachine } {}
void print(Document& doc) override {
m_printer.print(doc);
}
void scan(Document& doc) override {
m_scanner.scan(doc);
}
void fax(Document& doc) override {
m_faxmachine.fax(doc);
}
};
}
static void test_anti_conceptual_example_isp()
{
using namespace InterfaceSegregationPrinciple01;
Document doc{};
MultiFunctionPrinter printer;
printer.fax(doc); // works
Scanner scanner;
scanner.fax(doc); // does not work !!!
}
static void test_conceptual_example_isp()
{
using namespace InterfaceSegregationPrinciple02;
Document doc;
Printer printer;
Scanner scanner;
FaxMachine faxmachine;
Machine machine{ printer , scanner , faxmachine };
machine.scan(doc); // works
machine.fax(doc); // works too
}
void test_isp()
{
test_anti_conceptual_example_isp();
test_conceptual_example_isp();
}
// ===========================================================================
// End-of-File
// ===========================================================================