Learn

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 the Trainer‘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

1.

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.

2.

Change the Amplify() method name to Speak().

When you run the code, you’ll see an error in the Trainer class.

3.

Resolve the error by updating the Trainer class to use the new Speak() method.

4.

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.

5.

Resolve the error by updating the Trainer class to use the QuietSpeaker class throughout.

Sign up to start coding

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?