-
-
Save gadieichhorn/5033617b4ad44ae5cfe0434a88ef1898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class Service { | |
// call some external API | |
// the implementation can very without effecting the consumers | |
// it can be mocked for testing | |
string getData() { return string } | |
} | |
Class Consumer | |
// 1. service is injected (I) not created internally or maintained by this class | |
// 2. consumer is dependent on service to operate (D) | |
// 3. service can be changed without letting the consumer know | |
Consumer(Service service) { // you should abstract an interface for service | |
// you can save the service and use it . | |
} | |
} | |
// when testing you can mock a test service | |
class MockService { | |
string getData { return "A"} // expected results are returned without a need to call the real service | |
} | |
// test the consumer with the mock service by injecting it instead of the real thing | |
new consumer( new mockService()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment