Here’s one problem with this situation: if the dependency changes, we also have to change the dependent class. This means that:
- The code base is harder to maintain.
- We can’t test classes individually (if we change
LoudSpeaker
, then theTrainer
‘s behavior will change even though we didn’t change any code in that class). - The system isn’t flexible enough to allow us to swap the dependency for something similar.
Instructions
In LoudSpeaker.cs, change the LoudSpeaker
method behavior by changing the exclamation point !
to a question mark ?
.
When you run the code, you’ll see that the Trainer
behavior changes.
Change the Amplify()
method name to Speak()
.
When you run the code, you’ll see an error in the Trainer
class.
Resolve the error by updating the Trainer
class to use the new Speak()
method.
In Trainer.cs, replace new LoudSpeaker()
with new QuietSpeaker()
.
QuietSpeaker
also has a Speak()
method, which sets the argument to lower case and surrounds it with ellipses (...
).
There will still be an error in Trainer
.
Resolve the error by updating the Trainer
class to use the QuietSpeaker
class throughout.