@@ -24,4 +24,53 @@ It might be tempting to just open up the `Guitar` class and add a flame pattern
24
24
throw up in our application.
25
25
26
26
Following OCP we create ` SuperCoolGuitarWithFlames ` by extending the Guitar class we can be sure that our existing
27
- application won't be affected.
27
+ application won't be affected.
28
+
29
+ ### Liskov Substitution
30
+
31
+ ** If class A is a subtype of class B, then we should be able to replace B with A without disrupting the behavior
32
+ of our program.**
33
+
34
+ Defined a ` Car ` interface, we have implemented a ` MotorCar ` . We want to introduce electric cars which by definition
35
+ is a car without the engine. ` ElectricCar ` is the implementation. This a violation of Liskov substitution principle.
36
+
37
+ One possible solution would be to rework our model into interfaces that take into account the engine-less state of
38
+ our ` Car `
39
+
40
+ ### Interface Segregation Principle
41
+
42
+ ** Larger interfaces should be split into smaller ones. By doing so, we can ensure that implementing classes only need
43
+ to be concerned about the methods that are of interest to them**
44
+
45
+ We define a ` BearKeeper ` class. As avid zookeepers, we're more than happy to wash and feed our beloved bears.
46
+ However, we're all too aware of the dangers of petting them.
47
+
48
+ Only the crazy zookeepers can do that. But with this implementation we force every zookeepers to pet bears.
49
+
50
+ We create 3 new interfaces ` BearCleaner ` , ` BearFeeder ` and ` BearPetter ` .
51
+
52
+ Now, thanks to interface segregation we can implement ` BearCarer ` and ` CrazyPerson ` .
53
+
54
+ ### Dependency Inversion Principle
55
+
56
+ ** The principle of Dependency Inversion refers to the decoupling of software modules. This way, instead of high-level
57
+ modules depending on low-level modules, both will depend on abstractions.**
58
+
59
+ In ` Windows98Machine ` class by declaring the ` StandardKeyboard ` and ` Monitor ` with the new keyword, we've tightly
60
+ coupled these 3 classes together
61
+
62
+ Not only does this make our Windows98Computer hard to test, but we've also lost the ability to switch
63
+ out our StandardKeyboard class with a different one should the need arise. And we're stuck with
64
+ our Monitor class, too.
65
+
66
+ To implement DI we:
67
+
68
+ - adding a more general Keyboard interface
69
+ - rewrite it to ` Windows98MachineDI `
70
+
71
+ Now our classes are decoupled and communicate through the Keyboard abstraction. If we want, we can easily switch out
72
+ the type of keyboard in our machine with a different implementation of the interface.
73
+ We can follow the same principle for the Monitor class.
74
+
75
+ We've decoupled the dependencies and are free to test our Windows98Machine
76
+ with whichever testing framework we choose.
0 commit comments