-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolid.cpp
348 lines (277 loc) · 7.12 KB
/
solid.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* Single Responsibility Principle: Each class is responsible for implementing only one algorithm or functionality.
* Open/Closed Principle: A class and its methods should be open for extension but closed for modification.
* Liskov Substitution Principle: Replacing a parent object with a child object should not affect the program's behavior.
* Interface Segregation Principle: A class should not implement interfaces it does not need.
* Dependency Inversion Principle: High-level modules should not depend on low-level modules but rather on abstractions.
עקרון האחריות היחידה* (Single Responsibility Principle): כל מחלקה אחראית ליישום פונקציה או אלגוריתם אחד בלבד.
עקרון פתוח/סגור* (Open/Closed Principle): מחלקה ומתודות שלה צריכות להיות פתוחות להרחבה אך סגורות לשינויים.
עקרון החלפת ליסקוב* (Liskov Substitution Principle): החלפת אובייקט אב באובייקט בן לא תפגע בהתנהגות התוכנית.
עקרון הפרדת ממשקים* (Interface Segregation Principle): מחלקה לא תממש ממשקים שהיא אינה זקוקה להם.
עקרון היפוך תלות* (Dependency Inversion Principle): מודולים ברמה גבוהה לא יהיו תלויים במודולים ברמה נמוכה, אלא באבסטרקציות.
* עקרון האחריות היחידה (Single Responsibility Principle): כל מחלקה אחראית ליישום פונקציה או אלגוריתם אחד בלבד.
*
*
*
*לא
class Report {
*public:
* void generateReport() {
* // Logic for generating the report
* }
*
* void saveReportToFile(const std::string& filename) {
* // Logic for saving the report to a file
* }
*
* void printReport() {
* // Logic for printing the report
* }
*};
*
*
*
*
*
*
class ReportGenerator {
public:
std::string generateReport() {
// Logic for generating the report
return "Report Content";
}
};
class ReportSaver {
public:
void saveReportToFile(const std::string& report, const std::string& filename) {
// Logic for saving the report to a file
}
};
class ReportPrinter {
public:
void printReport(const std::string& report) {
// Logic for printing the report
}
* עקרון פתוח/סגור (Open/Closed Principle): מחלקה ומתודות שלה צריכות להיות פתוחות להרחבה אך סגורות לשינויים.
*
*
*לא
*
*class Employee {
*public:
* virtual double calculateSalary() const = 0;
* virtual ~Employee() = default;
*};
*
*class FullTimeEmployee : public Employee {
*public:
* double calculateSalary() const override {
* return 5000;
* }
*};
*
*class PartTimeEmployee : public Employee {
*public:
* double calculateSalary() const override {
* return 2000;
* }
*};
*
*class SalaryCalculator {
*public:
* double calculate(const Employee& employee) {
* return employee.calculateSalary();
* }
*};
*
*
*
*
*
class Employee {
public:
virtual double calculateSalary() const = 0;
virtual ~Employee() = default;
};
class FullTimeEmployee : public Employee {
public:
double calculateSalary() const override {
return 5000;
}
};
class PartTimeEmployee : public Employee {
public:
double calculateSalary() const override {
return 2000;
}
};
class SalaryCalculator {
public:
double calculate(const Employee& employee) {
return employee.calculateSalary();
}
};
* עקרון החלפת ליסקוב (Liskov Substitution Principle): החלפת אובייקט אב באובייקט בן לא תפגע בהתנהגות התוכנית.
*
*
*
* לא
* class Rectangle {
*protected:
* int width;
* int height;
*public:
* void setWidth(int w) { width = w; }
* void setHeight(int h) { height = h; }
* int getWidth() const { return width; }
* int getHeight() const { return height; }
* int area() const { return width * height; }
*};
*
*class Square : public Rectangle {
*public:
* void setWidth(int w) override {
* width = w;
* height = w; // שובר את החוזה של Rectangle
* }
* void setHeight(int h) override {
* height = h;
* width = h; // שובר את החוזה של Rectangle
* }
*};
*
*
*
class Shape {
public:
virtual int area() const = 0;
virtual ~Shape() = default;
};
class Rectangle : public Shape {
protected:
int width;
int height;
public:
Rectangle(int w, int h) : width(w), height(h) {}
int area() const override { return width * height; }
};
class Square : public Shape {
private:
int side;
public:
Square(int s) : side(s) {}
int area() const override { return side * side; }
};
* עקרון הפרדת ממשקים (Interface Segregation Principle): מחלקה לא תממש ממשקים שהיא אינה זקוקה להם.
*
*
*
*
* לא
* class Worker {
*public:
* virtual void work() = 0;
* virtual void eat() = 0;
*};
*
*class HumanWorker : public Worker {
*public:
* void work() override {
* // Work logic for a human
* }
* void eat() override {
* // Eating logic for a human
* }
*};
*
*class RobotWorker : public Worker {
*public:
* void work() override {
* // Work logic for a robot
* }
* void eat() override {
* // What should a robot do here? Robots don't eat!
* }
*};
*
*
*
*
*
class Workable {
public:
virtual void work() = 0;
virtual ~Workable() = default;
};
class Eatable {
public:
virtual void eat() = 0;
virtual ~Eatable() = default;
};
class HumanWorker : public Workable, public Eatable {
public:
void work() override {
// Work logic for a human
}
void eat() override {
// Eating logic for a human
}
};
class RobotWorker : public Workable {
public:
void work() override {
// Work logic for a robot
}
};
* עקרון היפוך תלות (Dependency Inversion Principle): מודולים ברמה גבוהה לא יהיו תלויים במודולים ברמה נמוכה, אלא באבסטרקציות.
*
*
*
*
*
*
*לא
*class FileLogger {
*public:
* void log(const std::string& message) {
* // Logic to log message to a file
* }
*};
*
*class Application {
* FileLogger logger; // תלות ישירה במימוש ספציפי
*public:
* void process() {
* // Logic
* logger.log("Processing started...");
* }
*};
*
*
class ILogger {
public:
virtual void log(const std::string& message) = 0;
virtual ~ILogger() = default;
};
class FileLogger : public ILogger {
public:
void log(const std::string& message) override {
// Logic to log message to a file
}
};
class ConsoleLogger : public ILogger {
public:
void log(const std::string& message) override {
// Logic to log message to the console
}
};
class Application {
ILogger& logger; // תלות באבסטרקציה, לא במימוש ספציפי
public:
Application(ILogger& logger) : logger(logger) {}
void process() {
// Logic
logger.log("Processing started...");
}
};
**/