-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingle_responsibility.java
57 lines (46 loc) · 1.42 KB
/
single_responsibility.java
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
// A class that tries to do everything
class Text {
String text;
String author;
int length;
String getText() { ... }
void setText(String s) { ... }
String getAuthor() { ... }
void setAuthor(String s) { ... }
int getLength() { ... }
void setLength(int k) { ... }
/*methods that change the text*/
void allLettersToUpperCase() { ... }
void findSubTextAndDelete(String s) { ... }
/*method for formatting output*/
void printText() { ... }
}
// better implementation that separates logic with presentation
class Text {
String text;
String author;
int length;
String getText() { ... }
void setText(String s) { ... }
String getAuthor() { ... }
void setAuthor(String s) { ... }
int getLength() { ... }
void setLength(int k) { ... }
/*methods that change the text*/
void allLettersToUpperCase() { ... }
void findSubTextAndDelete(String s) { ... }
}
class Printer {
Text text;
Printer(Text t) {
this.text = t;
}
void printText() { ... }
}
/*
In the second example we have divided the responsibilities of editing text
and printing text between two classes. You can notice that, if an error occurred,
the debugging would be easier, since it wouldn’t be that difficult to recognize
where the mistake is. Also, there is less risk of accidentally introducing
software bugs, since you’re modifying a smaller portion of code.
*/