Skip to content

Commit cb172e7

Browse files
author
ccarella
committed
adding final version ocp
1 parent 08993a1 commit cb172e7

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

README.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1-
Design Principles
1+
### Design Principles
22

3-
Solid Java
3+
## Solid Java
4+
5+
Design principles encourage us to create more maintainable, understandable, and flexible software. Consequently,
6+
as our applications grow in size, we can reduce their complexity
7+
8+
### Single Responsability
9+
10+
**A class should only have one responsibility. Furthermore, it should only have one reason to change.**
11+
12+
In `BookV1` the print method violates the SRP. For this reason the `BookV2` is amended to remove the method and
13+
`BookPrinter` is implemented a separate class that is concerned only with printing.
14+
15+
Not only have we developed a class that relieves the Book of its printing duties, but we can also leverage our
16+
`BookPrinter` class to send our text to other media.
17+
18+
### Open for Extension, Closed for Modification
19+
20+
**Classes should be open for extension, but closed for modification.**
21+
22+
After the first implementation of `Guitar`, we want to add in a second implementation, a flame pattern.
23+
It might be tempting to just open up the `Guitar` class and add a flame pattern – but who knows what errors that might
24+
throw up in our application.
25+
26+
Following OCP we create `SuperCoolGuitarWithFlames` by extending the Guitar class we can be sure that our existing
27+
application won't be affected.

src/main/java/ocp/Guitar.java

-11
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,12 @@ public String getMake() {
1616
return make;
1717
}
1818

19-
public void setMake(String make) {
20-
this.make = make;
21-
}
22-
2319
public String getModel() {
2420
return model;
2521
}
2622

27-
public void setModel(String model) {
28-
this.model = model;
29-
}
30-
3123
public int getVolume() {
3224
return volume;
3325
}
3426

35-
public void setVolume(int volume) {
36-
this.volume = volume;
37-
}
3827
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package ocp;
2+
3+
public class SuperCoolGuitarWithFlames extends Guitar {
4+
5+
private String flameColor;
6+
7+
public SuperCoolGuitarWithFlames(String make, String model, int volume) {
8+
super(make, model, volume);
9+
}
10+
11+
public String getFlameColor() {
12+
return flameColor;
13+
}
14+
}

0 commit comments

Comments
 (0)